I am using this pretty simple class without using any inheritance.
class A
{
int a;
int b;
public:
A(int x, int y) { a = x; b = y;}
A() :A(0,0){};
~A(){};
} ;
int main ()
{
A a1, a2(5, 7) ;
}
I get this error.
error C2614: 'A' : illegal member initialization: 'A' is not a base or member
There are similar questions on SO but they relate to inheritance. Can someone explain the reason and what does standard say about that?
EDIT:
It would be better if someone elaborate more on the forwarding constructor and this feature in C++11.
If you can use C++11, you could initialize
A()
fromA(int, int)
. This is not possible in C++03, where you have to write two separate constructors.If you want your code to work in C++03, you have two options:
init(int, int)
and call it from each of your constructors. This is a good choice if your constructor does a lot of work.You can also call a base constructor from a child class constructor. For instance, if you have
You could write
This is what your compiler means when it says that
A is not a base
, it is expecting this situation.All of these are compatible with C++03.
You could also upgrade your compiler to support C++11 features. I wouldn't recommend this if you are working in Linux and want your project to compile in Windows because Windows compilers don't implement all the C++ features that Linux compilers do (unless you pay for a good compiler).