The code was given, and it was necessary to answer what it would display:

class Base { public: virtual void someMethod() const { std::cout << "Base" << std::endl; } }; class Derived : public Base { public: void someMethod() const { std::cout << "Derived" << std::endl; } }; int main( int argc, char* argv[] ) { Derived o; Base* po = &o; Base bo = o; po->someMethod(); bo.someMethod(); return 0; } 

What will be displayed on the screen? I answered correctly:

 Derived Base 

In the first case, polymorphism. We look at the object on the right and during the method call the real type of the object is determined on the right and the table of virtual functions looks. In the second case, we simply have a base class object that knows nothing about the derivative. How to explain? Does the polymorphism type work only on pointers? How to explain this moment correctly?

  • For the test, add the copy constructor in Base, and see if it is called when Base bo = o; - timka_s
  • one
    Polymorphism works not only on pointers. - yapycoder

2 answers 2

I apologize for interfering after a brilliant answer from a respected @ Kitty wants to eat. I would just like to say that polymorphism can not appear differently than on pointers, since the base class can be and, in most cases, is abstract and, therefore, it is not possible to create an object of the base class. Here is a classic example of polymorphism (dynamic linking).

In the header file:

 class Shape{ ............ public: //или protected: virtual void Paint()=0;//абстрактный класс }; class Circle : public Shape{ void Paint(){....} }; class Rectangle : public Shape{ void Paint(){....} }; class Square : public Shape{ void Paint(){....} }; 

Somewhere in the codes:

 Shape *MyShapes[3];//Только так! - массив указателей на базовый класс! MyShapes[0] = new Circle; MyShapes[1] = new Rectangle; MyShapes[2] = new Square; //и затем..... for(int i = 0; i < 3; i++) MyShapes[i]->Paint();//рисует каждую фигуру 
  • 2
    By the way, a little about this classic example of polymorphism with Shape , Square , Circle and others. Good reasoning on why it’s impossible to find this way in production [ here. ] [1] [1]: insidecpp.ru/art/8 - Costantino Rupert
  • 2
    1. The question was about polymorphism and inheritance, and you and I answered it. 2. From the note you sent, I understood only that the base class should be well thought out so as not to burden the heirs. If there are any other meanings, explain them, please. In short, I demand the continuation of the banquet :). - BuilderC
  • 2
    I would like to add that it was only the bewilderment of the distinguished @mikillskegg about pointers to the base class that inspired me into the polemics. - BuilderC
  • 2
    @BuilderC Well, I didn’t fully understand the question about pointers, and the point of this article is that in normal production code, if you can replace inheritance with a certain property of an object, then you should do just that. Ie specifically, this example with circles, rectangles, etc. is made using a single Paint method and a set of corresponding points for drawing. And so, for educational purposes, an example, naturally, has the right to life. - Costantino Rupert
  • 3
    Gentlemen, this was neither bewilderment nor objection. Just for a long time with this did not deal, that's forgotten, and then the case turned up to refresh knowledge. - skegg

In the first case, a classic call of a virtual function occurs according to the corresponding pointer. By the way, it was possible to do the same with the help of the link:

 const Base& po = o; po.someMethod(); 

In the second case there will be an undesirable phenomenon called slicing.

The essence of the cut-off phenomenon lies in the fact that not quite obvious copying of objects occurs, and in this case copying only part of the Base . That is, in all the objects created in this way, the superfluous information, which constitutes the “essence” of the class Derived , will be irretrievably lost.


Other cut examples can be found here.

  • 2
    Features of virtual functions appear only when working with pointers? - skegg
  • 2
    @mikillskegg Yes, given that at a more "low" level, the link is the same pointer. It is impossible to make a virtual function call from an object entirely created on the stack (for obvious reasons, this is what happens in the second case in a given question) . - Costantino Rupert