I have chicken<->egg problem.
I want to create interface for a container classes, so that I can force implementation of specific attr-accessors and other methods and at the same time I want to be able to call those methods from the Containers constructors.
But as we know calling virtual method from Constructor will call the Base class methods, because of the sequence at which the constructors in hierarchy are called.
So the problem is I need virtual methods to force implementation, but that is the thing I can’t call in constructor.
EDIT: I want when I have to implement Container class (C1,C2…) to be forced to implement specific methods AND second I want to be able to use those methods in the Container-constructors. In simple terms I want to implement INTERFACE requirement, declaration-only-mixin. thanks
How do you solve this problem ?
Below is code sample.
#include <iostream>
using namespace std;
class Base {
public:
virtual void myvirt() { cout << "Base::virt()" << endl; };
};
class C1 : public Base {
public:
C1() {cout << "C1()" << endl; C1::myvirt(); };
C1(int i) { cout << "C1(int)" << endl; }
void myvirt() { cout << "C1::virt" << endl;}
};
class C2 : public Base {
public:
C2() {cout << "C2()" << endl; C2::myvirt();};
C2(int i) { cout << "C2(int)" << endl; }
void myvirt() { cout << "C2::virt" << endl;}
};
template<class C = C1>
class Sc: public C {
public:
Sc() : C() { cout << "Sc()" << endl; };
Sc(int n): C(n) { cout << "Sc(int)" << endl; };
};
int main() {
Sc<C2> $sx(12);
return 0;
}
Just as I’m asking is there some way to force implementation in descendant classes of specific Constructors().
Something like “virtual Constructors”.
Just make the constructor trivial and then after the constructor has returned, call some kind of initialization function to complete the construction of the object. You can also use the clone idiom, depending on how you want to decide what kind of object to construct.
You can also have a wrapper class whose constructor first constructs the inner class and then calls its virtual initialization function.
Update: You can wrap like this:
Now you can just do
And you can use
foojust like anInner.