Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get polymorphic behavior in a C++ constructor?

I have a base class that I want to look like this:

class B
{
    // should look like: int I() { return someConst; }
    virtual int I() = 0;
    public B() { something(I()); }
}

The point being to force deriving classes to override I and force it to be called when each object is constructed. This gets used to do some bookkeeping and I need to know what type of object is being constructed (but I otherwise treat the current object as the base class).

This doesn't work because C++ won't let you call an abstract virtual function from the constructor.

Is there a way to get the same effect?


Based on this link it would seem that the answer is there is no way to get what I want. However what it says is:

The short answer is: no. A base class doesn't know anything about what class it's derived from—and it's a good thing, too. [...] That is, the object doesn't officially become an instance of Derived1 until the constructor Derived1::Derived1 begins.

However in my case I don't want to know what it is but what it will become. In fact, I don't even care what I get back as long as I the user can (after the fact) map it to a class. So I could even use something like a return pointer and get away with it.

(now back to reading that link)

like image 491
BCS Avatar asked Sep 21 '09 06:09

BCS


People also ask

Can constructors be polymorphic?

No, constructor overriding is not possible in Java, so whether it is runtime polymorphism or not is out of question.

Is a constructor an example of polymorphism?

Constructors get executed in run time, but constructor overloading is an example of compile time polymorphism.

What is polymorphism in C language?

Master C and Embedded C Programming- Learn as you go Polymorphism is a key feature of object oriented programming that means having multiple forms. This is divided into compile time polymorphism and runtime polymorphism in C++. An example of compile time polymorphism is function overloading or operator overloading.

What makes a class polymorphic?

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class.


1 Answers

You can't call virtual methods from the constructor (or to be more precise, you can call them, but you'll end up calling the member function from the class currently being constructed)., the problem is that the derived object does not yet exist at that moment. There is very little you can do about it, calling virtual methods from the constructor polymorphically is simply out of the question.

You should rethink your design -- passing the constant as an argument to the constructor, for example.

class B
{
public:
    explicit B(int i)
    {
        something(i);
    }
};

See C++ faq for more. If you really want to call virtual functions during construction, read this.

like image 119
avakar Avatar answered Oct 20 '22 00:10

avakar