调用基类的构造函数/析构函数 [英] Call constructor/destructor of base class

查看:90
本文介绍了调用基类的构造函数/析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的问题如下:


如果有一个X类和一个Y类派生自X. br />

X的构造函数是X(param1,param2)。


Y的构造函数是Y(param1,...,param4)。


如何在我的剩余代码之前从Y的

构造函数/析构函数中调用X的构造函数/析构函数b $ b的构造函数/析构函数。


我可以使用''this''关键字。它被编译为析构函数但不是
的构造函数。 (((X *)this) - > X(...))

从Y的构造函数调用X的构造函数时,使用X(...)

被编译,但似乎无法正常工作,因为我在
运行时出现分段错误。


非常感谢任何建议。


FabianMüller


Hi all,

my question is as follows:

If have a class X and a class Y derived from X.

Constructor of X is X(param1, param2) .

Constructor of Y is Y(param1, ..., param4) .

How can I call the constructor/destructor of X from the
constructor/destructor of Y, before I the rest of the code of
constructor/destructor of Y.

Can I use the ''this'' keyword. It gets compiled for the destructor but doen''t
for the constructor. ( ((X*) this)->X( ... ) )

When calling the constructor of X from the constructor of Y, with X(...) it
gets compiled, but seems not to work, because I get segmentation errors at
runtime.

Many thanks to any advice.

Fabian Müller


推荐答案

"FabianMüller"写道:
"Fabian Müller" wrote:

大家好,

我的问题如下:

如果有一个类X和一个Y类派生自X.

X的构造函数是X(param1,param2)。

Y的构造函数是Y(param1,...,param4)。
构造函数/析构函数中调用X的构造函数/析构函数,然后才能使用Y /

的构造函数/析构函数的其余代码。
你没有明确地称之为''构造函数。编译器自动将构造函数称为

作为对象创建的副作用。


您可以从
$指示编译器使用哪个构造函数b $ b基类,通过在初始化列表中指定它。


class X

{

public:

X(int param1,int param2){}

};


class Y:public X

{

public:

Y(int param1,int param2,int param3):X(param2,param3){}

};

我可以使用''this''关键字。它是为析构函数编译的,但不是构造函数。 (((X *)this) - > X(...))

从Y的构造函数调用X的构造函数时,使用X(...)它
得到编译,但似乎不起作用,因为我在运行时得到分段错误。

Hi all,

my question is as follows:

If have a class X and a class Y derived from X.

Constructor of X is X(param1, param2) .

Constructor of Y is Y(param1, ..., param4) .

How can I call the constructor/destructor of X from the
constructor/destructor of Y, before I the rest of the code of
constructor/destructor of Y.
You don''t ''call'' a constructor explicitely. Constructors are called
automatically by compiler as a side effect of object creation.

You can instruct the compiler which constructor to use from the
base class, by specifiying it in the initialiser list.

class X
{
public:
X( int param1, int param2 ) {}
};

class Y : public X
{
public:
Y( int param1, int param2, int param3 ) : X( param2, param3 ) {}
};

Can I use the ''this'' keyword. It gets compiled for the destructor but doen''t
for the constructor. ( ((X*) this)->X( ... ) )

When calling the constructor of X from the constructor of Y, with X(...) it
gets compiled, but seems not to work, because I get segmentation errors at
runtime.




以上都是胡说八道。构造函数是一个没有名称的函数,因此程序员无法直接调用它。每当创建一个对象时,编译器都会向构造函数插入

调用。


但是所有这些,包括初始化列表,都应该在你的

课本。有什么可说的?


-

Karl Heinz Buchegger
kb ****** @ gascad.at


>
我可以使用''this''关键字。它被编译为析构函数,但
不用于构造函数。 (((X *)this) - > X(...))
Can I use the ''this'' keyword. It gets compiled for the destructor but doen''t for the constructor. ( ((X*) this)->X( ... ) )




不要这样做,析构函数会自动调用*。如果你自己打电话给它/ b $ b,那么它会被叫两次。


john



Don''t do that, the destructor gets called *automatically*. If you call it
yourself then its going to get called twice.

john


你好,


谢谢我在网上得到了同样的结果。


但是有一件事对我来说不了:


创建对象时,将调用

基类的构造函数/析构函数的顺序。

可以说X来自Y和Y是从Z派生的。

在对象创建时首先称为Z的构造函数,最后是X的构造函数,这对我有意义吗?


是否也会自动调用基类的析构函数,

构造函数?


I使用教科书MSDN Library,我认为这并不多。


再次感谢。


FabianMüller


" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息中写道

news:40 *************** @ gascad.at ...
Hi,

thanks I get the same result in the meantime by the web.

But one thing is missing for me:

When an object is created, in what order the constructors/destructors of the
base classes are called.
Lets say X is derived from Y, and Y is derived from Z.
At Object creation is first called the constructor of Z and at last the
constructor of X, which would make sense to me ?

Are the destructors of the base classes also be called automatically, as are
the constructors ?

I use the textbook "MSDN Library", which says not much to this, I think.

Many thanks again.

Fabian Müller

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
"FabianMüller"写道:
"Fabian Müller" wrote:

大家好,

我的问题如下:

如果有一个类X和一个Y类派生自X.

X的构造函数是X(param1,param2)。

Y的构造函数是Y(param1,...,param4)。

如何在Y的构造函数/析构函数的其余代码之前从Y的
构造函数/析构函数中调用X的构造函数/析构函数。

Hi all,

my question is as follows:

If have a class X and a class Y derived from X.

Constructor of X is X(param1, param2) .

Constructor of Y is Y(param1, ..., param4) .

How can I call the constructor/destructor of X from the
constructor/destructor of Y, before I the rest of the code of
constructor/destructor of Y.



你没有明确地称之为构造函数。构造函数由编译器自动调用,作为对象创建的副作用。

您可以通过在初始化程序中指定编译器,从
基类指示编译器使用哪个构造函数。列表。

类X
公共:
X(int param1,int param2){}
};
Y类:公共X
公开:
Y(int param1,int param2,int param3):X(param2,param3){}
};



You don''t ''call'' a constructor explicitely. Constructors are called
automatically by compiler as a side effect of object creation.

You can instruct the compiler which constructor to use from the
base class, by specifiying it in the initialiser list.

class X
{
public:
X( int param1, int param2 ) {}
};

class Y : public X
{
public:
Y( int param1, int param2, int param3 ) : X( param2, param3 ) {}
};


我可以使用''this''关键字。它被编译为析构函数,但
不用于构造函数。 (((X *)this) - > X(...))

从Y的构造函数调用X的构造函数时,使用X(...)
得到编译,但似乎不起作用,因为我在运行时得到分段错误

Can I use the ''this'' keyword. It gets compiled for the destructor but doen''t for the constructor. ( ((X*) this)->X( ... ) )

When calling the constructor of X from the constructor of Y, with X(...) it gets compiled, but seems not to work, because I get segmentation errors at runtime.



以上都是废话。构造函数是一个没有名称的函数,因此程序员无法直接调用它。每当创建一个对象时,编译器都会向构造函数插入
调用。

但是所有这些,包括初始化列表,都应该在你的
教科书中处理。有什么可说的?

-
Karl Heinz Buchegger
kb ****** @ gascad.at



这篇关于调用基类的构造函数/析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆