Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doubt in one disadvantage of doing work in constructors

Tags:

c++

I was reading Google C++ Style Guide, and got confused in the Doing Work in Constructors part. One of the cons of doing heavy work in constructor is:

If the work calls virtual functions, these calls will not get dispatched to the subclass implementations. Future modification to your class can quietly introduce this problem even if your class is not currently subclassed, causing much confusion.

I didn't understand what it means. Could someone provide an explanation and why this may be considered a disadvantage?

like image 441
kunigami Avatar asked Jul 21 '10 20:07

kunigami


2 Answers

I'm blatantly ripping off some example code from the Wikipedia Virtual function page:

#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 when you create a Wolf or a Fish object, since the Animal constructor will complete before the subclass object is initialized, the overriding eat functions won't exist yet.

This is a disadvantage because it can cause confusion between what's expected and what actually happens. If I override eat then create an object of my subclass, I expect my overridden function to be called, even from an Animal reference. I expect it because that's what happens when the call is made explicitly by code outside the constructor. The behavior is different inside the constructor, causing me to scratch my head in bewilderment.

like image 90
Bill the Lizard Avatar answered Sep 24 '22 15:09

Bill the Lizard


When an object is being constructed, the constructors for base classes are called first. Since the derived class hasn't been initialized yet, any calls to virtual methods during the base class constructor won't have a derived class object to work on.

like image 31
Cogwheel Avatar answered Sep 23 '22 15:09

Cogwheel