Constructor in fact is an usual function except that
10 No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value. The address of a constructor shall not be taken
Perhaps someone could outline what should be done in constructors, rather than mention what can potentially done. AFAIK the main idea is that constructors are for initialising member variables, and really not for anything else - so no implementation code. I might start by mentioning these things:
1. Simple initialisation via the initialiser list, including calling base class constructors;
2. Complex initialisation via a call to an Initialise() function;
3. Complex initialisation of variables common to all the constructors via a call to an InitCommon() function;
4. Complex initialisation of variables specific to all this constructor via a call to a different Init() function.
With number 2, Google have this because they don't wish to use exceptions. If that is not the case then I don't see why this couldn't be in the constructor. But, one then has to make sure to handle any exceptions that may arise. One example of complex initialisation: In my current project, I have a bunch of staticconstexpr's
Any thing else should be in a different member function.
Hopefully I am much less discombobulated than yesterday, and am saying the right things, if not feel free to shoot down what I have mentioned 8+)
#include <iostream>
struct A
{
virtual ~A() {}
virtualvoid f() const = 0;
};
void A::f() const
{
std::cout << "I'm a pure virtual function.\n";
}
struct B : A
{
B()
{
std::cout << "Let look what will be if we call the pure virual function:)\n";
A::f();
}
void f() const
{
}
};
int main()
{
B();
}