Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegation and Calling Parent Class Constructors: How do I do both?

I was wondering something, regarding delegation of derived class constructors. What's the proper method for delegating constructors when you also have to call the parent class' constructor? I know you can't have both delegation and member initialisation in the same initialiser list, but I don't know if calling the parent class' constructor has the same limitation.

// Option 1: Call parent class constructor, then delegate:

class Foo {
    public:
        Foo(int);
};

class Bar : public Foo {
    public:
        Bar(int, float) : Foo(int), Bar(int, float, 'c');
        Bar(int, float, char);
};

// Option 2: Delegate, then call parent class constructor:

class Foo {
public:
    Foo(int);
};

class Bar : public Foo {
    public:
        Bar(int, float) : Bar(int, float, 'c'), Foo(int);
        Bar(int, float, char);
};

// Option 3: Primary constructor calls parent class constructor:

class Foo {
    public:
        Foo(int);
};

class Bar : public Foo {
    public:
        Bar(int, float) : Bar(int, float, 'c');
        Bar(int, float, char) : Foo(int);
};

I would assume it's either #1 or #3 (most likely #3), but I'm not sure; I know it's not a combination, since that would call Foo() twice. I don't think it's #2, but I listed it just to cover all the possibilities.

I know there's a bit of a weird property to delegation in C++, too, in that the class is considered to be constructed as soon as any of its constructors finishes executing, even if you delegated the call to a different one and it hasn't finished the one that was actually [i]called[/i] yet. I don't think that would have any effect in this case, but it's worth mentioning.

I searched the site and googled it, but couldn't find an exact answer; the closest I could find was the bit about not having delegation and initialisation in the same init list, on MSDN. I'm unable to run some test code through a fully C++11-compatible compiler right at the moment, so I couldn't just experiment. Help would be appreciated.

Thanks in advance.

like image 762
Justin Time - Reinstate Monica Avatar asked Oct 07 '15 22:10

Justin Time - Reinstate Monica


People also ask

Can we call constructor of parent in class?

For calling the constructor of a parent class we can use the super keyword. The super() method from the constructor method is used for the invocation of the constructor method of the parent class to get access to the parent's properties and methods.

How do you call a delegate constructor in C++?

However, C++ Constructor delegation provides an elegant solution to handle this problem, by allowing us to call a constructor by placing it in the initializer list of other constructors. The following program demonstrates how it is done: CPP.

Can you create a delegate to a constructor in C#?

Can you create a delegate to a constructor in C#? No. No, constructors are not quite the same things as methods and as such you cannot create a delegate that points to them.

What is constructor delegation?

Delegating constructors can call the target constructor to do the initialization. A delegating constructor can also be used as the target constructor of one or more delegating constructors. You can use this feature to make programs more readable and maintainable.


2 Answers

Option #3 is the only valid one. You can't have a base initializer and a delegation in the same init list for the same reason you can't have a member initializer and a delegation: how would the compiler know not to initialize the member/base twice?

like image 56
Sebastian Redl Avatar answered Oct 08 '22 02:10

Sebastian Redl


This is the only valid option:

class Bar : public Foo {
    public:
        Bar(int, float) : Bar(int, float, 'c') {}
        Bar(int, float, char) : Foo(int) {}
};

If your constructor delegates, then its mem-initializer-list is allowed to only consist of the single delegating constructor call.

like image 45
Barry Avatar answered Oct 08 '22 02:10

Barry