o******r 发帖数: 259 | 1 Code 来了
template
class PointGeneric
{
public:
inline PointGeneric(const T xx, const T yy);
inline PointGeneric();
inline virtual ~PointGeneric();
T x;
T y;
};
template
class PointGeneric3D : public PointGeneric
{
public:
...
// left shift
inline const PointGeneric3D operator-(const PointGeneric3D& p) co
nst;
inline PointGeneric3D(T xx, T yy, T zz);
...
inline PointGeneric3D();
| p***o 发帖数: 1252 | 2 基类不是那么初始化的。试试
: z(zz), PointGeneric(xx,yy)
【在 o******r 的大作中提到】 : Code 来了 : template : class PointGeneric : { : public: : inline PointGeneric(const T xx, const T yy); : inline PointGeneric(); : inline virtual ~PointGeneric(); : T x; : T y;
| t****t 发帖数: 6806 | 3 You are not supposed to CALL a ctor directly.
you should use initializer to "call" it.
i.e., if you don't write a initializer
PointGeneric3D::PointGeneric3D(T xx, T yy, T zz)
{ ... }
then compiler will automatically generate a initializer as if you wrote
PointGeneric3D::PointGeneric3D(....) : PointGeneric()
{ ... }
this implicit initializer will call ctor of base class and set the vptr
to the vtbl of base class.
after this implicit initializer, the ctor of derived class is called and
th
【在 o******r 的大作中提到】 : Code 来了 : template : class PointGeneric : { : public: : inline PointGeneric(const T xx, const T yy); : inline PointGeneric(); : inline virtual ~PointGeneric(); : T x; : T y;
| t****t 发帖数: 6806 | 4 vptr and vtbl is the basis of the whole virtual function system,
I assume you know it...
I know you wanted to call that! And I thought you know how to call that...
your way of calling base class ctor is INCORRECT and initializer should
be used. And initializer CAN be used to call ctor with parameter, just
write the parameter down, what do you expect... and pptwo already showed
you how to do that.
it probably looks fine, but it is incorrect, period.
base class ctor with parameter should not be c
【在 o******r 的大作中提到】 : Code 来了 : template : class PointGeneric : { : public: : inline PointGeneric(const T xx, const T yy); : inline PointGeneric(); : inline virtual ~PointGeneric(); : T x; : T y;
| o******r 发帖数: 259 | 5 Thanks for replying.
I checked the books and the right way is:
template
PointGeneric3D::PointGeneric3D(const T xx, const T yy, const T zz)
{
}
I know virtual functions, just don't remember so many details as you do.
Thank you all for clear this confusion for me. | t****t 发帖数: 6806 | 6 Yes let me tell you why this worked but directly using PointGeneric3D
doesn't.
To make it simple, I will rename your class (template doesn't matter):
PointGeneric -> A
PointGeneric3D -> B
PointInt -> C
So you have
class A {
A() {}
A(short xx, short yy) {...} // *******first
virtual ~A() {}
short x, y;
};
class B : public A {
B() {}
short z;
B(short xx, short yy, short zz) : z(zz) { this -> A::A(xx,yy); } // ***second
// I even didn't know you could write above, cr
【在 o******r 的大作中提到】 : Thanks for replying. : I checked the books and the right way is: : template : PointGeneric3D::PointGeneric3D(const T xx, const T yy, const T zz) : { : } : I know virtual functions, just don't remember so many details as you do. : Thank you all for clear this confusion for me.
| t****t 发帖数: 6806 | 7
the good way is to reverse the order, because base class ctors are
always initialized first, no matter in what order did you write them
【在 o******r 的大作中提到】 : Thanks for replying. : I checked the books and the right way is: : template : PointGeneric3D::PointGeneric3D(const T xx, const T yy, const T zz) : { : } : I know virtual functions, just don't remember so many details as you do. : Thank you all for clear this confusion for me.
|
|