Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

c++ - Correct way of initializing a struct in a class constructor

So I want to add a struct from a c header file as a class member to a c++ class. But I get a compiler error for the cpp file: bar was not declared inn this scope. This is what I have:

//    myClass.hpp
#include fileWithStruct.h

class myClass
{
    public:
        struct foo bar;
};


//myClass.cpp

#include "myClass.hpp"

//Initialize structure in Constrcutor
myClass::myClass(  )
{
    bar = {1, 0, "someString", 0x4};
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

C++03 Style

#include "fileWithStruct.h"
/* say the contents were
struct foo
{
   int foo1;
   float foo2;
};
*/

class myClass
{
    public:
        int val;
        foo bar;
        // since foo is a POD-struct (a.k.a C struct), no constructor would be present
        // however bar() will zero-initialize everything in the struct
        myClass() : val(), bar()
        {
        }
};

The parentheses following bar matters. Refer value and zero-initialization to understand why this works. It is to be noted that by adding a constructor to myClass, we've made it a non-POD type. To work around this, one can retain myClass as an aggregate and write:

class myClass
{
    public:
        int val;
        foo bar;
};

int main()
{
   myClass zeroed_obj = { };
   myClass inited_obj = { 2, {0, 1.0f} };
   myClass partially_inited_obj = { 2 };    // equivalent to {2, {}}; which would zero all of myClass::bar
   myClass garbage_obj;    // warning: when left uninitialized, every member without a constructor will end up with garbage value
}

C++11 Style

class myClass
{
public:
   // default member initializations
   int val = { };         // zero-initialization
   foo bar = { 0, 0.0f }; // aggregate-initializing foo here, just giving { } will zero all of myClass::bar

   // should you want to receive an element from the constructor, this can be done too
   // aggregate initializing a struct in constructor initialization list is allowed from C++11 onwards
   // in C++03, we would've resorted to just setting the member of bar inside the constructor body
   myClass(int _foo1) : val{}, bar{_foo1, 0.f}
   {
   }

   // since we've a non-default constructor, we've to re-introduce the default constructor
   // if we need the above in-class initialization to work
   myClass() = default;
};

Here we use C++11's uniform initialization syntax. However, by doing this myClass becomes a non-POD type; member initialization is akin to adding a constructor to the class, thereby rendering myClass a non-trivial but standard-layout class. As per C++11 for a class to be POD it should be both trivial and standard-layout. Instead doing

#include "fileWithStruct.h"
#include <type_traits>
#include <iostream>

class myClass
{
public:
   int val;
   foo bar;
};

int main()
{
    myClass obj { }; // initializes val, bar.foo1 and bar.foo2 to 0
    myClass m { 0, {1, 2.0f} }; // initilizes each member separately
    std::cout << std::is_pod<myClass>::value << std::endl; // will return 1
}

will retain myClass as a POD.

Refer to this excellent post to know more about aggregates and PODs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...