Redian新闻
>
Re: VC里面的stl支持是不是很弱?
avatar
Re: VC里面的stl支持是不是很弱?# Programming - 葵花宝典
o*r
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();
avatar
p*o
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;

avatar
t*t
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;

avatar
t*t
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;

avatar
o*r
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.
avatar
t*t
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.

avatar
t*t
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。