简体   繁体   English

初始化类的struct data成员

[英]Initializing struct data members of a class

typedef struct _MY_STRUCT

{

         std::string     mystring;        
         int             n1;
         int             n2;
} MY_STRUCT;

class foo

{

public:

     foo():
         m_mystruct()
{ }

private:

     MY_STRUCT m_mystruct;
};

int main(void)

{

    foo oFoo;              
    // Why doesnt this intialize all data members of foo to NULL/0.
    // what is the correct way to get all members of MY_STRUCT to be intialized to NULL/0.   
}

First, you don't need to do typedef like this in C++. 首先,您不需要在C ++中使用这样的typedef Second, create a default constructor for your structure: 其次,为您的结构创建一个默认构造函数:

struct MY_STRUCT
{
    std::string mystring;
    int n1;
    int n2;

    MY_STRUCT() : mystring(), n1(), n2() {}
};

This way the structure members will be default-initialized to: 这样,结构成员将默认初始化为:

  • std::string to empty string (via its default constructor), std::string为空字符串(通过其默认构造函数),
  • int s to zero. int s为零。

So the following holds: 以下是:

MY_STRUCT ms;
assert( ms.mystring.empty());
assert( ms.n1 == 0 );
assert( ms.n2 == 0 );

You could do just add a constructor to your struct definition: 你可以只在构造定义中添加一个构造函数:

typedef struct _MY_STRUCT

{
    _MY_STRUCT()
    {
        n1 = 0;
        n2 = 0;
    }
    std::string     mystring;        
    int             n1;
    int             n2;
} MY_STRUCT;

The reason is most probably that you're using Visual C++. 原因很可能是您使用的是Visual C ++。 Even as of version 10.0 Visual C++ does not do value initialization correctly. 即使从版本10.0开始,Visual C ++也没有正确地进行值初始化。 Your code works fine with g++ 4.4.1. 您的代码适用于g ++ 4.4.1。

Around 2005 it was understandable that Visual C++ wasn't quite up to par, because the C++98 rules for initialization were changed in C++03. 在2005年左右,可以理解Visual C ++并不完全可以接受,因为在C ++ 03中改变了初始化的C ++ 98规则。 This was the only language change in C++03, which otherwise was just a "technical corrigendum" of C++98 (C++03 is sometimes called TC1, technical corrigendum 1). 这是C ++ 03中唯一的语言变化,否则它只是C ++ 98的“技术勘误”(C ++ 03有时称为TC1,技术勘误1)。 C++03 introduced "value initialization" as a generalization of "default initialization", in order to make the result less arbitrary and less baffling and just more practical for aggregate classes like yours, classes containing both POD and non-POD members: with C++03 rules those members are zero-initialized or default-initialized as appropriate, all of them. C ++ 03引入了“值初始化”作为“默认初始化”的一般化,为了使结果不那么随意而且不那么令人困惑,对于像你这样的聚合类,包含POD和非POD成员的类更加实用: C ++ 03规定那些成员是零初始化或默认初始化,所有这些成员。 And it was a very good thing. 这是一件非常好的事情。 T'was Andrew Koenig who proposed this, IIRC, and it weights up for his blame for Koenig Lookup (aka ADL, Argument Dependent Lookup). T'是Andrew Koenig,提出了这个,IIRC,并且它加重了对Koenig Lookup(又名ADL,Argument Dependent Lookup)的责任。 :-) :-)

But as of 2010 it's a bit less understandable that Visual C++ doesn't do this correctly. 但截至2010年,Visual C ++无法正确执行此操作也不太可理解。

That said, your code is horrible. 那就是说,你的代码太可怕了。 :-) :-)

See the other comments for improvements to the code, including the idea of defining a constructor, which will fix the problem for Visual C++. 请参阅其他注释以了解代码的改进,包括定义构造函数的想法,这将解决Visual C ++的问题。

Cheers & hth., 干杯&hth。,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM