C++ 03-Art
#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()
{
}
};
Die parentheses following bar
matters. Sehen Sie sich value and zero-initialization an, um zu verstehen, warum das funktioniert. Es ist zu beachten, dass wir durch Hinzufügen eines Konstruktors zu myClass
einen Nicht-POD-Typ erstellt haben. Um dies zu umgehen, kann man myClass
als Aggregat erhalten und schreiben:
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-Art
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) : bar{_foo1, 0.f}, val{}
{
}
// 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;
};
Hier verwenden wir C++ 11 die uniform initialization syntax. Dadurch wird myClass
jedoch ein Nicht-POD-Typ; Die Elementinitialisierung ähnelt dem Hinzufügen eines Konstruktors zur Klasse, wodurch myClass
eine nicht-triviale Standardlayoutklasse dargestellt wird. Gemäß C++ 11 für eine Klasse, die POD sein soll, sollte es sowohl trivial als auch Standard-Layout sein. Stattdessen
#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
}
wird behalten myClass
als POD.
Weitere Informationen zu Aggregaten und PODs finden Sie unter this excellent post.
Sie n nicht: Sie haben nicht zu C++ 11 noch bewegt eed to sat 'struct foo bar;' in C++ 03 oder C++ 11 genügt 'foo bar;'. In C++ 11 können Sie auch 'bar' zum Zeitpunkt der Deklaration initialisieren. – juanchopanza
Einverstanden, fixierte es in der Antwort. Das ist eine alte C-style-Deklaration für Strukturobjekte. – legends2k
Das OP erwähnt ausdrücklich, dass "foo" in einem C-Header enthalten ist, daher ist das Hinzufügen eines Konstruktors keine Option. (Und bitte beheben Sie das nicht, indem Sie '#ifdef __cplusplus' in die C-Kopfzeile setzen) –