关于成员变量初始化和默认构造函数问题 [英] About member variable initilization and default constructor issues

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

问题描述

在Inside C ++对象模型中,Lippman说有四种情况,如果构造函数不存在,

compile将合成一个默认构造函数来初始化成员

变量:


1.有虚拟功能;


2.虚拟继承;


3带有显式默认构造函数的.base类;


4.使用显式默认构造函数的成员对象。


例如


A级{


公开:


int i;


A * p;

}

int main()

{

A a;

if(ai == 0 || ap == 0)//做点什么

}


对于上述情况,行为未定义。如果我们将A类修改为

有四个characeristics中的任何一个,那么成员变量

将在默认构造函数中初始化,由
$ b表示。 $ b编译器,好像

A():i(0),p(0){}在类中定义。


但我试试这在不同的编译器中,产生不同的结果。我的

问题是:Lippman教授ISO标准,还是编译器 -

依赖?谢谢

In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.

e.g.

class A {

public:

int i;

A* p;
}
int main()
{
A a;
if(a.i ==0 || a.p == 0) //do something
}

The behavior is undefined for the above case. If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.

But I try this in different compilers, and yield different results. My
question is : Is Lippman teaching ISO standard, or compiler-
dependent? Thanks

推荐答案

JosephLee写道:
JosephLee wrote:

In C ++对象模型,Lippman说有四种情况,如果构造函数不存在,

compile会合成一个默认构造函数来初始化成员

变量:


1.有一个虚函数;


2.虚拟继承;


3.base类,带显式默认构造函数;


4.使用显式默认构造函数构成对象。


例如


class A {


public:


int i;


A * p;

}

int main()

{

A a;

if(ai == 0 | | ap == 0)//做点什么

}


对于上述情况,行为未定义。如果我们将A类修改为

有四个characeristics中的任何一个,那么成员变量

将在默认构造函数中初始化,由
$ b表示。 $ b编译器,好像

A():i(0),p(0){}在类中定义。


但我试试这在不同的编译器中,产生不同的结果。我的

问题是:Lippman教授ISO标准,还是编译器 -

依赖?谢谢
In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.

e.g.

class A {

public:

int i;

A* p;
}
int main()
{
A a;
if(a.i ==0 || a.p == 0) //do something
}

The behavior is undefined for the above case. If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.

But I try this in different compilers, and yield different results. My
question is : Is Lippman teaching ISO standard, or compiler-
dependent? Thanks



标准说如果构造函数隐含地定义了
并且该类没有虚函数或虚基,那么构造函数是微不足道的,所有直接

基类都有琐碎的c-tors,所有非静态数据成员也都有b
琐碎的c-tors。所以,转过来,你会得到Lippman所说的。


V

-

请删除大写''A'当用电子邮件回复时

我没有回复最热门的回复,请不要问

The Standard says that the constructor is trivial if it''s implicitly
defined and the class no virtual functions or virtual bases, all direct
base classes have trivial c-tors, all non-static data members also have
trivial c-tors. So, turn that around and you get what Lippman says.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask





Victor Bazarov写道:
Hi

Victor Bazarov wrote:

JosephLee写道:
JosephLee wrote:

>在Inside C ++对象Model中,Lippman说有四种情况,如果构造函数不存在,
compile会合成一个默认构造函数来初始化成员
变量:

1。有一个虚拟功能;

2。虚继承;

3.base类具有显式默认构造函数;

4.member具有显式默认构造函数的对象。
>In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.



[...]

[...]


> ;如果我们将A类修改为具有四个characeristics中的任何一个,那么成员变量
将在由
编译器sythesys化的默认构造函数中初始化,就好像
A ():i(0),p(0){}在类中定义。
>If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.



标准说如果构造函数隐含地定义为
并且类没有虚函数或虚拟基,那么构造函数是微不足道的直接

基类有琐碎的c-tors,所有非静态数据成员也都有b
琐碎的c-tors。所以,转过来,你会得到Lippman说的话。


The Standard says that the constructor is trivial if it''s implicitly
defined and the class no virtual functions or virtual bases, all direct
base classes have trivial c-tors, all non-static data members also have
trivial c-tors. So, turn that around and you get what Lippman says.



我不同意。首先,如果1-4不满足则不正确那么

将没有合成的默认构造函数:


如果有的话没有用户声明的类X的构造函数,默认构造函数

是隐式声明的。


"类的隐式声明的默认构造函数在用于创建其类类型的对象时,隐式地定义了


隐式定义的默认构造函数执行初始化的集合

将由用户编写的默认构造函数执行

for那个带有空的mem-initializer-list和一个空函数的类

body"


其次,后一个短语解释了在上述两个案例中,

程序的行为就好像有一个用户定义的构造函数


A(){}


这意味着我和p都不会被初始化。


Markus

I disagree. Firstly, it''s not correct that if 1-4 is not satisfied then
there will be no default-constructor synthesized:

"If there is no user-declared constructor for class X, a default constructor
is implicitly declared."

"An implicitly-declared default constructor for a class is implicitly
defined when it is used to create an object of its class type. The
implicitly-defined default constructor performs the set of initializations
of the class that would be performed by a user-written default constructor
for that class with an empty mem-initializer-list and an empty function
body"

Secondly, the latter phrase explains that in both of the above cases, the
program will behave as if there was a user-defined constructor

A() {}

This means that neither i nor p will be initialized.

Markus


Markus Moll写道:
Markus Moll wrote:




Victor Bazarov写道:
Hi

Victor Bazarov wrote:

> JosephLee写道:
>JosephLee wrote:

>>在Inside C ++对象模型中,Lippman说
中有四种情况,编译将sythesize默认构造函数初始化
会员va如果构造函数不存在则会出现问题:

1。有一个虚拟功能;

2。虚继承;

3.base类具有显式默认构造函数;

4.member具有显式默认构造函数的对象。
>>In Inside C++ object Model, Lippman said there are four cases in
which compile will sythesize a default constructor to initialize
the member variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.



[...]

[...]


> ;>如果我们修改A类到
有四个characeristics中的任何一个,那么成员变量
将在
编译器sythesysized的默认构造函数中初始化,就好像 A():i(0),p(0){}在类中定义。
>>If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.


标准说如果构造函数被隐式定义并且类没有虚函数或虚函数,所有
直接基类都有,那么构造函数是微不足道的琐碎的c-tors,所有非静态数据成员也有微不足道的c-tors。所以,转过来,你会得到什么
Lippman说。


The Standard says that the constructor is trivial if it''s implicitly
defined and the class no virtual functions or virtual bases, all
direct base classes have trivial c-tors, all non-static data members
also have trivial c-tors. So, turn that around and you get what
Lippman says.



我不同意。首先,如果1-4不满足则不正确

则不会合成默认构造函数:


如果有的话没有用户声明的类X的构造函数,默认声明了一个默认的

构造函数。


一个隐式声明的类的默认构造函数在用于创建其类类型的对象时,隐式地定义了


隐式定义的默认构造函数执行该类的初始化,该初始化将由

用户编写的默认构造函数执行。那个带有空的

mem-initializer-list和一个空函数体的类'


其次,后一个短语解释了在上述两种情况中,

程序的行为就好像有一个用户定义的构造函数


A(){}


这意味着i和p都不会被初始化。


I disagree. Firstly, it''s not correct that if 1-4 is not satisfied
then there will be no default-constructor synthesized:

"If there is no user-declared constructor for class X, a default
constructor is implicitly declared."

"An implicitly-declared default constructor for a class is implicitly
defined when it is used to create an object of its class type. The
implicitly-defined default constructor performs the set of
initializations of the class that would be performed by a
user-written default constructor for that class with an empty
mem-initializer-list and an empty function body"

Secondly, the latter phrase explains that in both of the above cases,
the program will behave as if there was a user-defined constructor

A() {}

This means that neither i nor p will be initialized.



对。我不确定你在和谁争论。在

类中导致隐式定义的非平凡c-tor的四个特定的

特征与标准中的相同。 Lippman说,如果存在四个中的任何一个,那么将产生c-tor
。 Stadard说,如果这四个人中没有一个存在,那么c-tor是微不足道的。从那个意义上来说,李普曼教授标准。不是吗?


V

-

请在通过电子邮件回复时删除资金'A' />
我没有回复最热门的回复,请不要问

Right. I am not sure with whom you''re arguing. The four specific
traits that cause an implicitly defined non-trivial c-tor in the
class are the same as in the Standard. Lippman says that the c-tor
is generated if any of the four is present. The Stadard says that
the c-tor is trivial if none of the four is present. In that sense
Lippman teaches the Standard. No?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于关于成员变量初始化和默认构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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