由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Re: VC里面的stl支持是不是很弱?
相关主题
question about const referencec++问题,请高人指点迷津,c++ faq lite的一个例子
问个copy constructor的问题C++ (direct vs indirect initialization)
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?c++面试问题
Cannot use my own container as the underlying container of a stack? (c++)one question about initializaiton list
文一个简单的c++question regarding const function
[合集] 请问-fno-implicit-templates的用处vptr 到底是在第一个还是最后一个
请叫一个 template class constructor 的问题[合集] 关于template和inheritance的问题请教
C++ question一个C++ template的问题
相关话题的讨论汇总
话题: inline话题: class话题: const
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
一个C++ template的问题文一个简单的c++
一道c++的考古题[合集] 请问-fno-implicit-templates的用处
请教C++问题请叫一个 template class constructor 的问题
c++ questionC++ question
question about const referencec++问题,请高人指点迷津,c++ faq lite的一个例子
问个copy constructor的问题C++ (direct vs indirect initialization)
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?c++面试问题
Cannot use my own container as the underlying container of a stack? (c++)one question about initializaiton list
相关话题的讨论汇总
话题: inline话题: class话题: const