oop - Why we can't implement polymorphism in C++ without base class pointer or reference? -




Take a look at the following code first (this code is the base class and the line derived class in the shape ) ->

The upper part of the zero (size sarre [], integer size) {for (int i = 0; i


When we compile this program, the draw method of shape will be called first, then the program will end. Why it ended? Why can not we apply polymorphism without base class pointers or what is the technical reason for this? If we are trying to implement polymorphism from the array of objects, then what compiler will it do? Please explain with examples very wisely. I will be very grateful

You are asking a question and providing a code example that fails to separate Due to the words of your question:

Why do references / polymorphism are needed for polymorphism?

  struct base {virtual zero f (); }; Derivative structure: Public base {virtual zero} (F); }; Zero call 1 (base b) {B.F. (); // base: F} zero call 2 (base & amp; b) {bf}; // Derivative :: f} int main () {derived d; Call1 (d); Call2 (d); }  

When you use pass-by-value semantics (or store the elements found in a base container), you can type the elements of the base Type copies are being generated generated . This is called slicing action because it is similar to the fact that you have the generated object and you slice / cut only Subject to base in the example, the call1 object does not work from the d object, rather than a temporary type base And from base: F .

In the call2 method you are going in the context of the base object. When the compiler sees call2 (d) in the main it will make reference in the base sub-theme d and pass it to the function. Function operates on the basis of a reference type of base that indicates an object to derived , and derived :: f Will call. When you receive base * in a derived object, then the object is still derived .

Why can not I put a container of generated pointers in a function taking the containers of base points?

If the derived is base , base is a container of derived base in containers of em>

No derived will break the system type base of the container Type system break in form The stuff down.

  void f (std:: vector & lt; base * & gt; & amp; v) {v.push_back (new base); V.push_back (new another_dirvit); } Int main () {std :: vector & lt; Derived * & gt; V; F (V); // Error !!! }  

If the line marked with an error was allowed by the language, then it will allow the application to insert elements that are generated * type

But the question was about price types of containers ...

When you have containers of value type, element in container If copied, by inserting an element of type derived , type The type of base is base of the object as it is above slice except for a language restriction, it is for a good reason , When you have a container for the base objects, you have only one place to hold the base elements, you can not store large objects in the same container. Other compilers will not even know how much space will be reserved for each element (if we expand later with any major type)

In other languages ​​it really permits (Java), but it is not. The only change in the syntax is when you want to enter the string array [] in Java, then you actually want to put it in the string * array [] in C ++ all non- There are references in the primitive type language, and the fact is that you do not add * to the syntax, this does not mean that the example string in the container, the containers reference < / Em> in strings, which are related to c ++ references.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -