Doubt about one lack of work in designers

I read the Google C ++ style guide and got confused about the Doing Work in Constructors part . One of the disadvantages of doing heavy work in the constructor:

If the job calls virtual functions, these calls will not be sent to the subclass implementation. Future modification of your class may calmly address this problem, even if the class is not currently subclassed, causing a lot of confusion.

I do not understand what this means. Can someone give an explanation and why can this be considered a disadvantage?

+7
c ++
source share
6 answers

I am frankly copying some sample code from Wikipedia Virtual Function :

#include <iostream> #include <vector> class Animal { public: virtual void eat() const { std::cout << "I eat like a generic Animal." << std::endl; } virtual ~Animal() { } }; class Wolf : public Animal { public: void eat() const { std::cout << "I eat like a wolf!" << std::endl; } }; class Fish : public Animal { public: void eat() const { std::cout << "I eat like a fish!" << std::endl; } }; 

If you call eat() inside the Animal constructor, it will call the Animal eat() function every time. Even if you create a Wolf or Fish object, since the Animal constructor will be completed before the subclass object is initialized, overriding eat functions will not exist yet.

This is a flaw, because it can cause confusion between what is expected and what is actually happening. If I override eat , then create an object of my subclass, I expect my overridden function to be called, even with the Animal link. I expect this because this happens when a call is made explicitly by code outside the constructor. The behavior inside the constructor is different from the other, which makes me confuse my head.

+9
source share

When an object is built, constructors for the base classes are first created. Since the derived class is not yet initialized, any calls to virtual methods in the constructor of the base class will not have an object of the derived class that it will work on.

+4
source share

When an instance of a subclass is created, it is first called the constructor of the base class, and then the constructor of the subclass.

If the constructor of the base class calls the virtual method, then the method of the base class will be called instead of the subclass, although the instance is a subclass. This can be a problem.

More information here: http://www.artima.com/cppsource/nevercall.html

+2
source share

If you inherit a class, the methods that you override / implement will not be called in this case. So, if Employee calls work () in the constructor, then later you will come with Hourly :: work () and SalariedEmployee :: work (), which will not be called. Although they have different implementations, they will still be considered as Employee, and not their special implementations.

+1
source share

The constructor's task is simply to initialize the initial state of your object. If you start to perform tasks that rely on actions taken in virtual functions, you may get unexpected results when subclasses begin to implement these functions.

0
source share

The behavior that virtual function calls are not virtual is well defined, but very surprising, so compilers usually give warnings.

0
source share

All Articles