sub class constructor calling its "super constructor"
Apr 12, 2010 at 4:47pm UTC
I have a super class and a sub class defined as follow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
class Super
{
public :
double x;
Super ( double x ): x(x)
{}
Super()
{
Super(0);
}
};
class Sub : public Super
{
public :
double y;
Sub ( double x, double y) : Super(x), y(y)
{
}
};
When I instantiate the Sub class
1 2 3
Sub mySub(1,2);
cout << mySub.x << endl << mySub.y << endl;
I'd get
as the output, which is obvious!
The thing is, when I tried to modify the Sub's constructor like this
1 2 3 4 5
Sub ( double x, double y )
{
Super(x);
this ->y = y;
}
I got an error that read
"error C2082: redefinition of formal parameter 'x'"
So! Can anyone explain what that means? And why there's a difference between two way of defining the constructor.
Thanks!
Apr 12, 2010 at 5:56pm UTC
Base class constructors can only be called via initializer list.
Topic archived. No new replies allowed.