member-initialization

Should trivial default constructor respect default member initializer here?

邮差的信 提交于 2019-12-07 06:40:27
问题 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

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+

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-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

工具导航Map

JSON相关