构造函数初始化列表 [英] constructor initialization list

查看:104
本文介绍了构造函数初始化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


有一个奇怪的例子,一个类Test由三个

其他类的对象组成。即


班级考试

{public:

Test();

private:

点x;

猫咪;

狗z;

};


类Point,Cat,Dog是


class Point

{public:

Point();

Point(Point& x,Dog& y);

private:

};


class Cat

{public:

Cat();

Cat(Point& x,Dog& y);

private:

};


class Dog

{public:

Dog() ;

狗(Point& x,Cat& y);

私人:

};


如果我们调用默认构造函数它们就没有问题了,因为在他的书中,他们按照他们的顺序调用了它们,这是在课堂上定义的
主要课程(测试)。


我的大问题是为什么以下工作正常?即当其他

构造函数被调用时。


Test :: Test():x(),y(x,z),z(x, y)

自创建对象y以来
与对象z的创建有关。


任何人都可以解释这种奇怪的行为吗?


提前谢谢,

Hello all,

There is a strange case where a class Test is composed from three objects of
other classes. i.e.

class Test
{ public:
Test();
private:
Point x;
Cat y;
Dog z;
};

where the classes Point, Cat, Dog are

class Point
{ public:
Point();
Point(Point& x, Dog& y);
private:
};

class Cat
{ public:
Cat();
Cat(Point& x, Dog& y);
private:
};

class Dog
{ public:
Dog();
Dog(Point& x, Cat& y);
private:
};

If we were calling the default constructors they is no problem, since as
STROUSTRUP is wrtiing in his book they are called in the order they are
defined in the class the main class (Test).

My big problem is why the following is working? i.e when the other
constructors are called.

Test::Test():x(), y(x,z), z(x,y)

since the creation of object "y" is related with the creation of object "z".

Can any body give an explanation of this strange behaviour?

Thanks in advance,

推荐答案

任何机构都可以解释这种奇怪的行为吗?
Can any body give an explanation of this strange behaviour?



对象按照它们的顺序构建,在

初始化列表中。


对于*不列出*的对象在初始化列表中,它们是在*所有其他成员对象之前构造的* b $ b。所以,给出以下内容:

#include< string>


class Blah

{

public:

std :: string a;

std :: string b;

std :: string c;

std :: string d;

std :: string e;

std :: string f;


Blah ():c(Monkey!),e(),a(),f(Ape!);

};

成员对象在上面构建的顺序如下:


b

d

c
$ b $

a

f

-JKop


The objects are contructed in the order in which they are, in the
Initialization List.

For objects that *aren''t* listed in the initialization list, they are
constructed *before* all other member objects. So, given the following:
#include <string>

class Blah
{
public:
std::string a;
std::string b;
std::string c;
std::string d;
std::string e;
std::string f;

Blah() : c("Monkey!"), e(), a(), f("Ape!");
};
The order of in which the member objects are consructed in the above is as
follows:

b
d
c
e
a
f
-JKop


Test :: Test():x(),y(x,z),z(x,y)
Test::Test():x(), y(x,z), z(x,y)



Opps!我在上一篇文章中错过了这一点。

以上代码格式错误。

-JKop


Opps! I missed the point with my last post.
The above code is ill-formed.
-JKop


Makis Papapanagiotou写道:
Makis Papapanagiotou wrote:

大家好,

有一个奇怪的例子,一个类Test由三个其他类的对象组成。即

上课测试
{public:
Test();
私人:
点x;
Cat y;
狗z;
};

其中课程Point,Cat,Dog是

类Point
{public:
Point() ;点(Point& x,Dog& y);
私人:
};

类Cat
{public:
Cat ();
Cat(Point& x,Dog& y);
私人:
};

班级狗
{public:
狗();
狗(Point& x,Cat& y);
私人:
};

如果我们调用默认构造函数他们不是问题,因为在他的书中,他们按照他们在课堂上定义的顺序(测试)来调用。

我的大问题是为什么以下是有效的吗?


定义''工作''

,即调用其他
构造函数时。


仍然是相同的订单:

x首先构建

y next next

z获取构造最后

Test :: Test():x(),y(x,z),z(x,y)

Hello all,

There is a strange case where a class Test is composed from three objects of
other classes. i.e.

class Test
{ public:
Test();
private:
Point x;
Cat y;
Dog z;
};

where the classes Point, Cat, Dog are

class Point
{ public:
Point();
Point(Point& x, Dog& y);
private:
};

class Cat
{ public:
Cat();
Cat(Point& x, Dog& y);
private:
};

class Dog
{ public:
Dog();
Dog(Point& x, Cat& y);
private:
};

If we were calling the default constructors they is no problem, since as
STROUSTRUP is wrtiing in his book they are called in the order they are
defined in the class the main class (Test).

My big problem is why the following is working?
Define ''working''
i.e when the other
constructors are called.
It is still the same order:
x gets constructed first
y gets constructed next
z gets constructed last

Test::Test():x(), y(x,z), z(x,y)




成员对象的构造函数的调用顺序

完全由它们在类声明中列出的顺序定义

。在这里,在初始化列表中

你只需指定*哪个*构造函数用于成员,

不是它们被调用的顺序。


-

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


这篇关于构造函数初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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