member-initialization

Should trivial default constructor respect default member initializer here?

北城以北 提交于 2019-12-05 09:31:59
Consider the code: #include <atomic> #include <iostream> struct stru { int a{}; int b{}; }; int main() { std::atomic<stru> as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here ), while gcc 7.2.0 seems so (see here ). Strictly speaking, I think both compilers are right, in that your program exhibits undefined

What is “member initializer” in C++11?

こ雲淡風輕ζ 提交于 2019-12-01 15:18:09
I run across a weird concept named "member initializer". Here says: C++11 added member initializers, expressions to be applied to members at class scope if a constructor did not initialize the member itself. What is its definition? Are there some examples to illustrate its usage? juanchopanza It probably refers to in-class member initializers. This allows you to initialize non-static data members at the point of declaration: struct Foo { explicit Foo(int i) : i(i) {} // x is initialized to 3.1416 int i = 42; double x = 3.1416; }; More on that in Bjarne Stroustrup's C++11 FAQ . You can now add

Right way to conditionally initialize a C++ member variable?

纵饮孤独 提交于 2019-11-30 06:57:03
I'm sure this is a really simple question. The following code shows what I'm trying to do: class MemberClass { public: MemberClass(int abc){ } }; class MyClass { public: MemberClass m_class; MyClass(int xyz) { if(xyz == 42) m_class = MemberClass(12); else m_class = MemberClass(32); } }; This doesn't compile, because m_class is being created with an empty constructor (which doesn't exist). What is the right way of doing this? My guess is using pointers and instantiating m_class using new , but I'm hoping there is an easier way. Edit: I should have said earlier, but my actual problem has an

Right way to conditionally initialize a C++ member variable?

喜欢而已 提交于 2019-11-29 07:57:57
问题 I'm sure this is a really simple question. The following code shows what I'm trying to do: class MemberClass { public: MemberClass(int abc){ } }; class MyClass { public: MemberClass m_class; MyClass(int xyz) { if(xyz == 42) m_class = MemberClass(12); else m_class = MemberClass(32); } }; This doesn't compile, because m_class is being created with an empty constructor (which doesn't exist). What is the right way of doing this? My guess is using pointers and instantiating m_class using new , but

Initializer list syntax in member initializer list using C++11

半世苍凉 提交于 2019-11-27 08:59:46
I've been going through ' A Tour of C++ ' and Bjarne uses the the c++11 initializer list feature in member initialization in a constructor, like so (using curly brackets): A a; B b; Foo(Bar bar): a{bar.a}, b{bar.b} {} This, however doesn't compile prior to c++11. What is the difference with the old member initializer list (using round brackets): Foo(Bar bar): a(bar.a), b(bar.b) {} So what is the difference and when should one be preferred over the other? So what is the difference? Round brackets only work for non-class types, or types with a suitable constructor for the number of arguments in

Initializer list syntax in member initializer list using C++11

本小妞迷上赌 提交于 2019-11-26 14:26:17
问题 I've been going through 'A Tour of C++' and Bjarne uses the the c++11 initializer list feature in member initialization in a constructor, like so (using curly brackets): A a; B b; Foo(Bar bar): a{bar.a}, b{bar.b} {} This, however doesn't compile prior to c++11. What is the difference with the old member initializer list (using round brackets): Foo(Bar bar): a(bar.a), b(bar.b) {} So what is the difference and when should one be preferred over the other? 回答1: So what is the difference? Round

How do C++ class members get initialized if I don&#39;t do it explicitly?

雨燕双飞 提交于 2019-11-26 04:04:24
问题 Suppose I have a class with private memebers ptr , name , pname , rname , crname and age . What happens if I don\'t initialize them myself? Here is an example: class Example { private: int *ptr; string name; string *pname; string &rname; const string &crname; int age; public: Example() {} }; And then I do: int main() { Example ex; } How are the members initialized in ex? What happens with pointers? Do string and int get 0-intialized with default constructors string() and int() ? What about