How can I fake constructor inheritance in C++03? - c++

As far as I know, you cannot inherit constructors in C++. But there are situations, where it might be required that it looks like you can instantiate inherited classes the same way you instantiate their base:
struct Base {
int i;
int const j;
Base(int i, int j) : i(i), j(j) {}
};
// option 1)
struct Derived1 : Base {
Derived1(int i, int j) : Base(i,j) {}
};
Base* baseFactory() {
return new Derived1(42,47);
}
// option 2)
struct Derived2 : Base {
};
Base* baseFactory() {
Base* b = new Derived2();
b->i = 42;
b->j = 47; // broken!
// alternative could be to implement Base::operator=
return b;
}
Note that the derived classes could be default constructed if it weren't for their base class(es).
Option 1 is what is usually done, I think, but you are typing code without expressing anything new. Option 2 breaks const correctness (and prevents you from using references as members) because everything must be assignable.
Is there a nice way around this?
Edit: C++11 is great, but unfortunately I need a C++03 solution.

Option 1 is the idiomatic way in C++03, it does require more typing, but that is how the language is. Option 2 is not even close to equivalent, you would not be able to initialize Derived2 as Base does not have a default constructor, and the definition of the implicitly declared default constructor for Derived2 needs it.
But beyond the technical difficulty in the language you are trading construction for two-phase construction which is never a good idea, and at the same time forcing your use of Derived2 to dynamically allocated objects.

There is an alternative approach:
// option 3)
struct Derived3 : Base {
Derived3(const Base &b) : Base(b) {}
};
Base* baseFactory3() {
return new Derived3(Base(42,47));
}
This may not be a good idea if constructing a full Base object is expensive or requires external resources. In that case you could create a light-weight object that carries constructor arguments for Base, so that instead of multiple Base constructors you have multiple BaseArguments constructors, and one Base constructor that takes in BaseArguments. However I don't think many people would consider that good style in most circumstances.

C++11 supports inherited constructors.
http://en.wikipedia.org/wiki/C%2B%2B11
However not all compilers that supports now a subset of C++11 supports all functionalities.
Microsoft visual C++ 2010 supports several new C++0x features but not inherited constructors, here is a list: http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx
GCC supports more new features but not the inherited constructors. See http://gcc.gnu.org/projects/cxx0x.html
To enable GCC to compile with C++0x features you need to add -std=c++0x or -std=gnu++0x compilation parameter.
Visual C++ 2010 have these enabled by default.
You just need to wait :)
There are not clean solutions for previous version of the standard, for this reason it was introduced in C++11, because it was not possible to do with previous versions.
So, the only thing you can do, is to copy your constructors and call base constructor.

Here' a variation on the Builder Pattern that I always thought it could be useful, but I never actually got a chance to use it; I guess you could call it the Constructor Pattern. :P
It also offers some other advantages, like not having to specify arguments in a certain order: you only specify what you are interested in and in whatever order you want.
I still think this is justified only if your class has lots of constructors with different parameters. Otherwise, it's just an annoyance.
class Person
{
public:
class Constructor;
};
class Person::Constructor
{
public:
Constructor& name(const std::string&);
Constructor& age(int);
Person* make();
};
Person* pers = Person::Constructor()
.name("Bob Marley").make();
class Employee : public Person
{
public:
class Constructor;
};
class Employee::Constructor : public Person::Constructor
{
public:
Constructor& salary(double);
Employee* make();
};
Employee* emp = Employee::Constructor()
.name("Emilly Smith").age(23).make();
But again, this is justifiable only if your class has lots of constructors with lots of parameters and you want to avoid having to write multiple overloads to those; otherwise, this just adds too much complexity without any real benefit.
I mentioned I don't like han's proposed solution. This is because (1) you are moving the need to (re)declare a number of constructors in the child class to the need to place the same number of functions inside a factory class; (2) it is hacky and the intent is not explicit; and (3) you could view this as a violation of dependency injection.

Related

Why should a derived class's constructor use the base's default constructor in it's initializer list?

Here is an example of my question:
class MyBaseClass
{
public:
MyBaseClass(): my_bool(false), my_value(0)
{}
MyBaseClass(bool b, int i): my_bool(b), my_value(i)
{}
private:
bool my_bool;
int my_value;
}
class MyDerivedClass1 : public ::MyBaseClass
{
public:
MyDerivedClass1(double d): my_double(d)
{}
private:
double my_double;
}
class MyDerivedClass2 : public ::MyBaseClass
{
public:
MyDerivedClass2(double d): MyBaseClass(), my_double(d)
{}
private:
double my_double;
}
Why isn't the MyDerivedClass1 an ok way to initialize my derived class versus having to explicitly initialize the base class like in MyDerivedClass2?
I guess I don't understand why I can't just rely on C++ calling my base constructor? I know if I wanted them initialized to something different I'd have to call the other constructor in my initialization list, but all I want is the base constructor to be called anyway.
There is no difference between providing a default-constructed base class in the initializer list or not providing it. What you use if entirely your style. (or the company)
In general, I would say you have 2 options (let's keep constructors out of scope), assuming you always initialize your members.
Option 1: Initialize all members in the init-list.
This option should be used if you are C++98-compatible. It has as advantage that you are defining all construction parameters in a single list, making it easy to search through. (Unless you have 50+ members)
The disadvantage of this option is a lot of duplication when you have multiple constructors.
With this, you can have 3 variants for them:
Skip the default-initialized classes, this makes the list shorter though it's hard to check if you have intended to 'forget' it. (Think replacing a class by a ptr to it)
Default initialize all members, this makes the list longer, though indicates clearly the intent
Explicitly provide the class you are initializing, by copy contructing with a tempuary
Option 2: Initialize all members on declaration, except for constructor parameters
This option assumes you initialize everything in the class declaration. Yet again you can explicitly call the default constructor or not, preferably with braced init, as round braces are interpreted as a function declaration.
In the initializer list you only have to put the members which are linked to the construction parameters.
The advantage of this option is readability (especially for large classes). The disadvantage is that this limits your compiler options to 'modern' compilers.
Base classes
If we again consider base classes and look at them as if they were members, the consistent way would be to declare them explicitely for option 1 and don't write them for option 2.
Personnally I like option 2, as I too often encounter classes with too many members and checking if all members are initialized is easier by hand with this one.
Option 1 however is often used because of legacy in order to keep the code consistent.
POD
Important to mention are the PODs (Plain old datatype), like int, double ...
If you don't initialize them you get some random data. This makes it important to explicitly intialize them, regardless of the method you use to do so.
Conclusion
So in the end, it's all a matter of style with no functional difference.
Although in most cases there isn't a semantic difference and the issue is mostly one of style, there is one case where it is a difference: when the default constructor of the base class is not user-provided.
The difference comes from the fact that a base that is not in the member initializer list is default-initialized, while explicitly writing Base() value-initializes it, which performs zero-initialization if the default constructor isn't user-provided.
Thus:
struct A { int i; };
struct B : A {
B() : j(i) {} // may be undefined behavior
int j;
};
struct C : A {
C() : A(), j(i) {} // OK; both i and j are zero
int j;
};

Method in superclass that returns instance of subclass

This question is the equivalent of Java's How to make Superclass Method returns instance of SubClass
Suppose this class hierarchy:
class A
{
public:
A makeCopyOfObject();
};
class B: public A
{
public:
void doSomethingB();
};
class C: public A
{
public:
void doSomethingC();
};
I'd like to use it this way:
B().makeCopyOfObject().doSomethingB();
C().makeCopyOfObject().doSomethingC();
But of course I can't, because the makeCopyOfObject function returns an instance of A, not an instance of a subclass.
I surely could write two versions of the function in the two subclasses, but the code would be the same, except for the return type, because all fields to be copied and modified are in the base class. So, is there an alternative?
I don't understand what you try to achieve with that and it is probably not an answer (see comments), but what you can do is a template (in detail a Curiously recurring template pattern).
template<typename CRTP>
class A {
public:
CRTP& makeCopyOfObject() {
return static_cast<CRTP&>(*this);
}
};
class B: public A<B> {
public:
void doSomethingB();
};
class C: public A<C> {
public:
void doSomethingC();
};
And use it as you wanted:
b.makeCopyOfObject().doSomethingB();
c.makeCopyOfObject().doSomethingC();
See running example here.
Sidenote:
Because of laziness, even the function is called makeCopyOfObject it does not create a copy, but returns a reference to the object. To do a copy you have to implement copy constructors and return a copy (search for clone pattern).
It's difficult to see any practical use case for this, but it's easy to do with a non-member function template:
template< class Type >
auto copy_of( Type const& o )
-> Type
{ return o; }
Then you can write
copy_of( B() ).doSomething();
to your hearts' content, instead of just writing
B().doSomething();
Enjoy.
For the academic issue of covariant methods, there is a lot to be said. You might check out answers to how to implement a clone method. Essentially, this is not supported by C++ except for type system support for reference or pointer result, so one must do it "manually", and the three common approaches are (1) fully manual implementation in each class, (2) using a macro that expands to necessary code in each class, and (3) a middle-man class that forwards arguments to base class constructors. With C++03 the third way was about the same order of difficulty as leveraging dominance in a virtual inheritance hierarchy, so with C++03 that was also an option to be mentioned. But it's just not in the right ballpark of practicality with C++11 and later, (1), (2) and (3) it is.
First, beware of slicing.
Second, if you want to make a copy of an object, may I suggest
the copy-constructor:
B b1{};
B b2{b1}.doSomethingB();
or even:
B{ B{} }.doSomethingB();
Also, if you want to preserve polymorphic behaviour, always
handle polymorphic classes via smart-pointers or references.
Note that C++ has value-semantics, while Java has reference-semantics, so design-patterns that are good for Java are not necessarily good for C++.

Dealing with protected/private constructor/destructor for a CRTP design?

Consider the following code:
#include <iostream>
#include <type_traits>
// Abstract base class
template<class Crtp>
class Base
{
// Lifecycle
public: // MARKER 1
Base(const int x) : _x(x) {}
protected: // MARKER 2
~Base() {}
// Functions
public:
int get() {return _x;}
Crtp& set(const int x) {_x = x; return static_cast<Crtp&>(*this);}
// Data members
protected:
int _x;
};
// Derived class
class Derived
: public Base<Derived>
{
// Lifecycle
public:
Derived(const int x) : Base<Derived>(x) {}
~Derived() {}
};
// Main
int main()
{
Derived d(5);
std::cout<<d.set(42).get()<<std::endl;
return 0;
}
If I want a public inheritance of Derived from Base, and if I don't want a virtual destructor in the base class, what would be the best keywords for the constructor (MARKER 1) and the destructor (MARKER 2) of Base to guarantee that nothing bad can happen ?
Whatever programming style you use, you can alwyas do something bad: even if you follow the best of the bestest guideline practice. That's something physical behind it (and relate to the impossibility to reduce the global entrophy)
That said, don't confuse "classic OOP" (a methodology) with C++ (a language), OOP inheritache (a relation) with C++ inheritance (an aggregation mechanism) and OOP polymorphism (a model) with C++ runtime and static polymorphism (a dispatching mechanism).
Although names sometime matches, the C++-things don't have to necessarily sevicing OOP-things.
Public inheritance from a base with some non-virtual methods is normal. and destructor is not special: just dont call delete on the CRTP base.
Unlike with classic OOP, a CRTP-base has different type for each of the deriveds, so having a "pointer to a base" is clueless since there is no "pointer to a common type". And hence the risk to call "delete pbase" is very limited.
The "protected-dtor paradigm" is valid only if you are programming OOP-inheritance using C++ inheritance for object managed (and deleted) though pointer-based polymorphism. If you are following other paradigms, those rules should not be treated in a literal way.
In your case, the proteced-dtor just deny you to create a Base<Derived> on the stack and to call delete on a Base*. Something you will never do, since Base with no "Dervied" has no sense to exist, and having a Base<Derived>* makes no sense since you can have just a Derived*, hence having both public ctor and dtor makes no particular mess.
But you can even do the opposite choice to have both ctor and dtor protected, since you will never construct a Base alone, since it always needs a Derived type to be known.
Because of the particular construction of CRTP, all the classical OOP stuff leads to a sort of "indifferent equilibrium", since there is no more the "dangerous usecase".
You can use them or not, but no particular bad-thing can happen. Not if you use object the way they had been designed to be used.
While your code works I find it odd to mark the destructor rather than the constructor as protected. Normally my reasoning would be that you want to prevent the programmer from accidentally creating a CRTP base object. It all comes down to the same of course, but this is hardly canonical code.
The only thing that your code prevents is the accidental deletion of a CRTP object via a base pointer – i.e. a case like this:
Base<Derived>* base = new Derived;
delete base;
But that is a highly artificial situation that won’t arise in real code since CRTP simply isn’t supposed to be used that way. The CRTP base is an implementation detail that should be completely hidden from the client code.
So my recipe for this situation would be:
Define a protected constructor.
Don’t define a destructor – or, if required for the CRTP semantic, define it as public (and non-virtual).
There's no problem, since the destructor is protected it means that client code can't delete a pointer to Base, so there's no problem with Base's destructor being non-virtual.

In C++, is there a consensus regarding default behavior vs explicit code?

I'm wondering if there's any reason to explicitly write code that does the same as default behavior of C++.
Here's some code:
class BaseClass
{
public:
virtual ~BaseClass() {}
virtual void f() { /* do something */ }
};
class ExplicitClass
: public BaseClass
{
public:
ExplicitClass()
: BaseClass() // <-- explicit call of base class constructor
{
// empty function
}
virtual ~ExplicitClass() {} // <-- explicit empty virtual destructor
virtual void f() { BaseClass::f(); } // <-- function just calls base
};
class ImplicitClass
: public BaseClass
{
};
I'm mainly curious in the realm of refactoring and a changing code base. I don't think many coders intend to write code like this, but it can endup looking like this when code changes over time.
Is there any point to leaving the code present in the ExplicitClass? I can see the bonus that it shows you what is happening, but it comes across as lint-prone and risky.
Personally I prefer to remove any code that is default behavior code(like ImplicitClass).
Is there any consensus favoring one way or the other?
There are two approaches to this issue:
Define everything even if compiler generates the same,
Does not define anything which compile will do better.
The believers of (1) are using rules like: "Always define Default c-tor, Copy C-tor, assignment operator and d-tor".
(1) believers think it is safer to have more than to miss something.
Unfortunately (1) is especially loved by our managers - they believe it is better to have than don't have. Sp such rules like this "always define big four" go to "Coding standard" and must be followed.
I believe in (2). And for firms where such coding standards exist, I always put comment "Do not define copy c-tor as compiler does it better"
As the question is about consensus, I can't answer, but I find ildjarn's comment amusing and correct.
As per your question whether there is a reason to write it like that, there is not as the explicit and implicit class behave the same. People sometimes do it for 'maintenance' reasons e.g. if derived f is ever implemented in a different way to remember to call the base class. I personally, don't find this useful.
Either way is fine, so long as you understand what really happens there, and the problems which may arise from not writing the functions yourself.
EXCEPTION-SAFETY:
The compiler will generate the functions implicitly adding the necessary throw conditions. For an implicitly created constructor, this would be every throw conditions from the base classes and the members.
ILL-FORMED CODE
There are some tricky cases where some of the automatically generated members functions will be ill-formed. Here is an example:
class Derived;
class Base
{
public:
virtual Base& /* or Derived& */
operator=( const Derived& ) throw( B1 );
virtual ~Base() throw( B2 );
};
class Member
{
public:
Member& operator=( const Member& ) throw( M1 );
~Member() throw( M2 );
};
class Derived : public Base
{
Member m_;
// Derived& Derived::operator=( const Derived& )
// throw( B1, M1 ); // error, ill-formed
// Derived::~Derived()
// throw( B2, M2 ); // error, ill-formed
};
The operator= is ill-formed because its throw directive should be at least as restrictive as its base class, meaning that it should throw either B1, or nothing at all. This makes sense as a Derived object can also be seen as a Base object.
Note that it is perfectly legal to have an ill-formed function as long as you never invoke it.
I am basically rewriting GotW #69 here, so if you want more details, you can find them here
it depends on how you like to structure and read the programs. of course, there are preferences and reasons for and against each.
class ExplicitClass
: public BaseClass
{
public:
initialization is very important. not initializing a base or member can produce warnings, rightly so or catching a bug in some cases. so this really starts to make sense if that collection of warnings is enabled, you keep the warning levels way up, and the warning counts down. it also helps to demonstrate intention:
ExplicitClass()
: BaseClass() // <-- explicit call of base class constructor
{
// empty function
}
an empty virtual destructor is, IME, statistically the best place to export a virtual (of course, that definition would be elsewhere if visible to more than one translation). you want this exported because there is a ton of rtti and vtable information that could end up as unnecessary bloat in your binary. i actually define empty destructors very regularly for this reason:
virtual ~ExplicitClass() {} // <-- explicit empty virtual destructor
perhaps it is a convention in your group, or it documents that this is exactly what the implementation is designed to do. this can also be helpful (subjective) in large codebases or within complex hierarchies because it can also help remind you of the dynamic interface the type is expected to adopt. some people prefer all declarations in the subclass because they can see all the class' dynamic implementation in one place. so the locality helps them in the event the class hierarchy/interface is larger than the programmer's mental stack. like the destructor, this virtual may also be a good place to export the typeinfo:
virtual void f() { BaseClass::f(); } // <-- function just calls base
};
of course, it gets hard to follow a program or rationale if you define only if qualified. so it can end up with some codebases easier to follow if you just stick to conventions because it is clearer than documenting why an empty destructor is exported at every turn.
a final reason (which swings both ways) is that explicit default definitions can increase and decrease build and link times.
fortunately, it's easier and unambiguous now to specify default and deleted methods and constructors.

Dependency on Derived class constructor problem

I am working on a legacy framework. Lets say 'A' is the base-class and 'B' is the derived class. Both the classes do some critical framework initialization. FWIW, it uses ACE library heavily.
I have a situation wherein; an instance of 'B' is created. But the ctor of 'A' depends on some initialization that can only be performed from 'B'.
As we know when 'B' is instantiated the ctor for 'A' is invoked before that of 'B'. The virtual mechanism dosen't work from ctors, using static functions is ruled-out (due to static-initialization-order-fiasco).
I considered using the CRTP pattern as follows :-
template<class Derived>
class A {
public:
A(){
static_cast<Derived*>(this)->fun();
}
};
class B : public A<B> {
public:
B() : a(0) {
a = 10;
}
void fun() { std::cout << "Init Function, Variable a = " << a << std::endl; }
private:
int a;
};
But the class members that are initialized in the initializer list have undefined values as they are not yet executed (f.e. 'a' in the above case). In my case there a number of such framework-based initialization variables.
Are there any well-known patterns to handle this situation?
Thanks in advance,
Update:
Based on the idea given by dribeas, i conjured-up a temporary solution to this problem (a full-fledged refactoring does not fit my timelines for now). The following code will demonstrate the same:-
// move all A's dependent data in 'B' to a new class 'C'.
class C {
public:
C() : a(10)
{ }
int getA() { return a; }
private:
int a;
};
// enhance class A's ctor with a pointer to the newly split class
class A {
public:
A(C* cptr)
{
std::cout << "O.K. B's Init Data From C:- " << cptr->getA() <<
std::endl;
}
};
// now modify the actual derived class 'B' as follows
class B : public C, public A {
public:
B()
: A(static_cast<C*>(this))
{ }
};
For some more discussion on the same see this link on c.l.c++.m. There is a nice generic solution given by Konstantin Oznobikhin.
Probably the best thing you can do is refactoring. It does not make sense to have a base class depend on one of its derived types.
I have seen this done before, providing quite some pain to the developers: extend the ACE_Task class to provide a periodic thread that could be extended with concrete functionality and activating the thread from the periodic thread constructor only to find out that while in testing and more often than not it worked, but that in some situations the thread actually started before the most derived object was initialized.
Inheritance is a strong relationship that should be used only when required. If you take a look at the boost thread library (just the docs, no need to enter into detail), or the POCO library you will see that they split the problem in two: thread classes control thread execution and call a method that is passed to them in construction: the thread control is separated from the actual code that will be runned, and the fact that the code to be run is received as an argument to the constructor guarantees that it was constructed before the thread constructor was called.
Maybe you could use the same approach in your own code. Divide the functionality in two, whatever the derived class is doing now should be moved outside of the hierarchy (boost uses functors, POCO uses interfaces, use whatever seems to fit you most). Without a better description of what you are trying to do, I cannot really go into more detail.
Another thing you could try (this is fragile and I would recommend against) is breaking the B class into a C class that is independent of A and a B class that inherits from both, first from C then from A (with HUGE warning comments there). This will guarantee that C will be constructed prior to A. Then make the C subobject an argument of A (through an interface or as a template argument). This will probably be the fastest hack, but not a good one. Once you are willing to modify the code, just do it right.
First, I think your design is bad if the constructor of a base class depends on the something done in the constructor in a derived. It really shouldn't be that way. At the time the constructor of the base class run, the object of the derived class basically doesn't exist.
A solution might be to have a helper object passed from the derived class to the constructor of the base class.
Perhaps Lazy Initialization does it for you. Store a flag in A, wether it's initialized or not. Whenever you call a method, check for the flag. if it's false, initialize A (the ctor of B has been run then) and set the flag to true.
It is a bad design and as already said it is UB. Please consider moving such dependencies to some other method say 'initialize' and call this initialize method from your derived class constructor (or anywhere before you actually need the base class data to be initialized)
Hmm. So, if I'm reading into this correctly, "A" is part of the legacy code, and you're pretty damn sure the right answer to some problem is to use a derived class, B.
It seems to me that the simplest solution might be to make a functional (non-OOP) style static factory function;
static B& B::makeNew(...);
Except that you say you run into static initialization order fiasco? I wouldn't think you would with this kind of setup, since there's no initialization going on.
Alright, looking at the problem more, "C" needs to have some setup done by "B" that "A" needs done, only "A" gets first dibs, because you want to have inheritance. So... fake inheritance, in a way that lets you control construction order...?
class A
{
B* pB;
public:
rtype fakeVirtual(params) { return pB->fakeVirtual(params); }
~A()
{
pB->deleteFromA();
delete pB;
//Deletion stuff
}
protected:
void deleteFromB()
{
//Deletion stuff
pB = NULL;
}
}
class B
{
A* pA;
public:
rtype fakeInheritance(params) {return pA->fakeInheritance(params);}
~B()
{
//deletion stuff
pA->deleteFromB();
}
protected:
friend class A;
void deleteFromA()
{
//deletion stuff
pA = NULL;
}
}
While it's verbose, I think this should safely fake inheritance, and allow you to wait to construct A until after B has done it's thing. It's also encapsulated, so when you can pull A you shouldn't have to change anything other than A and B.
Alternatively, you may also want to take a few steps back and ask yourself; what is the functionality that inheritance gives me that I am trying to use, and how might I accomplish that via other means? For instance, CRTP can be used as an alternative to virtual, and policies an alternative to function inheritance. (I think that's the right phrasing of that). I'm using these ideas above, just dropping the templates b/c I'm only expecting A to template on B and vice versa.