I've tried searching for articles that relate to this issue, but all I keep finding are articles about people who don't know how to define constructors/can't call the parent constructor/etc and I think this will cut straight to the chase.
The problem code is as follows:
#include <stdio.h>
class TestA
{
public:
TestA(){}
TestA(const TestA &Temp)
{
printf("TestA called!\n");
}
};
class TestB : public TestA
{
public:
TestB(){}
TestB(const TestA &Temp)
{
printf("TestB called!\n");
}
};
int main (const int ArgC, const char *ArgV[])
{
TestB Test;
TestB Test2(Test); //Calls TestA's constructor TestA(const TestA &Temp), instead of TestB's
return 0;
}
A running online example of the code:
http://www.ideone.com/s6npy
The above code (on the GCC compiler) creates the problem where TestB doesn't actually call TestB's constructor, but instead (stupidly) calls TestA's constructor (I say stupid because if TestA's was intended it could be called from within TestB's constructor), and completely bypasses TestB's constructor in the process (you can even comment out TestB's constructors and it will still compile without errors).
Obviously, given constructors can't be made virtual, and defining either or both of the constructors as explicit is redundant (as it's called explicitly), I cannot see (and cannot find an article on) how I can force the compiler/program to choose TestB's constructor (when constructing TestB) over TestA's constructor.
I'm personally surprised because reading a lot of posts regarding constructors, I got the distinct impression the subclass constructor should be called first, but I find this isn't what is happening.
How can I resolve this issue?