C ++:成员指针已初始化? [英] C++: member pointer initialised?

查看:113
本文介绍了C ++:成员指针已初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码示例应解释以下内容:

  A类
{
B * pB;
C * pC;
D d;

public:
A(int i,int j):d(j)
{
pC = new C(i, abc);
} //注意pB未初始化,例如pB(NULL)

...
};

显然,pB应该明确初始化为NULL才是安全的(并且是明确的),但是,构造A后pB的值是什么?它是否默认初始化(为零?)(即不确定以及内存中有什么)。我意识到C ++中的初始化有很多规则。



我认为它不是默认的初始化;在Visual Studio中以调试模式运行时,它已将pB设置为指向0xcdcdcdcd-这意味着内存已被新建(在堆上)但尚未初始化。但是,在释放模式下,pB始终指向NULL。这是偶然的,因此不值得依赖的吗?还是这些编译器为我初始化了它(即使它不在标准中)?在Solaris上使用Sun的编译器进行编译时,它似乎也为NULL。



我确实在寻找对标准的特定引用,以一种方式或另一种方式进行。 / p>

谢谢。

解决方案

以下是标准的相关段落:


12.6.2初始化基和成员[class.base.init]



4如果给定的非静态数据成员或
基类未由
mem-initializer-list中的mem-

初始值设定项id命名,则



-如果实体是(可能经过cv限定)
类类型(或其数组)或基类的非静态数据
成员,则该实体
类是非POD类,该实体是默认初始化的( dcl.init )。
如果实体是const限定类型的非静态数据成员,则
实体类应具有用户声明的默认构造函数。



-否则,该实体未初始化
。如果实体是
const限定类型或引用类型,或者是(可能是cv-quali-
fied)POD类类型(或其数组)包含(直接或间接地
)是const限定类型的成员,程序是
病态-
形成的。



在调用
的构造函数之后如果X的成员



既未在
构造函数的mem-initializers中指定,也未在

默认初始化中指定,则X类已完成,也没有在执行构造函数

的过程中初始化
,该成员具有
的不确定值。



Code sample should explain things:

class A
{
    B* pB;
    C* pC;
    D d;

    public : 
    A(int i, int j) : d(j)
    {
        pC = new C(i, "abc");
    } // note pB is not initialised, e.g. pB(NULL)

    ...
};

Obviously pB should be initialised to NULL explicitly to be safe (and clear), but, as it stands, what is the value of pB after construction of A? Is it default initialised (which is zero?) or not (i.e. indeterminate and whatever was in memory). I realise initialisation in C++ has a fair few rules.

I think it isn't default initialised; as running in debug mode in Visual Studio it has set pB pointing to 0xcdcdcdcd - which means the memory has been new'd (on the heap) but not initialised. However in release mode, pB always points to NULL. Is this just by chance, and therefore not to be relied upon; or are these compilers initialising it for me (even if it's not in the standard)? It also seems to be NULL when compiled with Sun's compiler on Solaris.

I'm really looking for a specific reference to the standard to say one way or the other.

Thanks.

解决方案

Here is the relevant passage fromt he standard:

12.6.2 Initializing bases and members [class.base.init]

4 If a given nonstatic data member or base class is not named by a mem-
initializer-id in the mem-initializer-list, then

--If the entity is a nonstatic data member of (possibly cv-qualified) class type (or array thereof) or a base class, and the entity class is a non-POD class, the entity is default-initialized (dcl.init). If the entity is a nonstatic data member of a const-qualified type, the entity class shall have a user-declared default constructor.

--Otherwise, the entity is not initialized. If the entity is of const-qualified type or reference type, or of a (possibly cv-quali- fied) POD class type (or array thereof) containing (directly or indirectly) a member of a const-qualified type, the program is ill- formed.

After the call to a constructor for class X has completed, if a member

of X is neither specified in the constructor's mem-initializers, nor
default-initialized, nor initialized during execution of the body of
the constructor, the member has indeterminate value.

这篇关于C ++:成员指针已初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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