initialization-list

Initializing vector<string> with double curly braces

天涯浪子 提交于 2019-12-05 16:13:28
Can someone explain the difference in behavior between initializing with double and single curly braces in the example below? Code #1: vector<string> v = {"a", "b"}; string c(v[0] + v[1]); cout << "c = " << c; cout << "c.c_str() = " << c.c_str(); Output #1: c = ab c.c_str() = ab Code #2: vector<string> v = {{"a", "b"}}; string c(v[0] + v[1]); cout << "c = " << c; cout << "c.c_str() = " << c.c_str(); Output #2: c = a\acke�Z\ c.c_str() = a Implicit conversion central. That's what's going on. vector<string> v = {"a", "b"}; You initialize the vector by providing an initializer list with two

C++ Constructor initialization list strangeness

本小妞迷上赌 提交于 2019-12-04 02:32:18
I have always been a good boy when writing my classes, prefixing all member variables with m_: class Test { int m_int1; int m_int2; public: Test(int int1, int int2) : m_int1(int1), m_int2(int2) {} }; int main() { Test t(10, 20); // Just an example } However, recently I forgot to do that and ended up writing: class Test { int int1; int int2; public: // Very questionable, but of course I meant to assign ::int1 to this->int1! Test(int int1, int int2) : int1(int1), int2(int2) {} }; Believe it or not, the code compiled with no errors/warnings and the assignments took place correctly! It was only

Zero-Initialize array member in initialization list

微笑、不失礼 提交于 2019-12-03 01:17:35
I have a class with an array member that I would like to initialize to all zeros. class X { private: int m_array[10]; }; For a local variable, there is a straightforward way to zero-initialize (see here ): int myArray[10] = {}; Also, the class member m_array clearly needs to be initialized, as default-initializing ints will just leave random garbage, as explained here . However, I can see two ways of doing this for a member array: With parentheses: public: X() : m_array() {} With braces: public: X() : m_array{} {} Are both correct? Is there any difference between the two in C++11? Initialising

Is std::move really needed on initialization list of constructor for heavy members passed by value?

给你一囗甜甜゛ 提交于 2019-12-02 17:04:37
Recently I read an example from cppreference.../vector/emplace_back : struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } My question: is this std::move really needed? My point is that this p_name is not used in the body of constructor, so, maybe, there is some rule in the language to use move semantics for it by default? That would be really annoying to add std::move on initialization list to every

Class member without a default constructor

…衆ロ難τιáo~ 提交于 2019-12-01 18:17:27
Suppose I have a class A without a default constructor, a factory method factoryA that returns an object of type A, and a class B that has A as its member. I know that in this case the member of type A of B has to be initialize in B's constructor initialization list. It is not entirely clear to me why so if someone could explain that to me it would be great. Also, what if the parameter to A's constructor needs to be computed inside of B's constructor, say by querying a database or something of that nature? Is there a way to use the setup below without providing A with a default constructor?

Is there a Way to Get Warned about Misbehaving Designated Initializers?

落花浮王杯 提交于 2019-12-01 08:38:19
C99 introduced the concept of designated intializers for structs. So for example, given: typedef struct { int c; char a; float b; } X; I could initialize like: X foo = {.a = '\1', .b = 2.0F, .c = 4}; and calling: printf("c = %d\na = %hhu\nb = %f", foo.c, foo.a, foo.b); would output: c = 4 a = 1 b = 2.000000 As mentioned here this has the "surprising behavior" of assigning to c then a then b , independent of the order of my designated initializers. This becomes a real issue if I have functions like this: int i = 0; int f() { return ++i; } int g() { i += 2; return i; } int h() { i += 4; return i

Initialize const member variables

↘锁芯ラ 提交于 2019-11-30 21:37:08
问题 I have C++ code that boils down to something like the following: class Foo{ bool bar; bool baz; Foo(const void*); }; Foo::Foo(const void* ptr){ const struct my_struct* s = complex_method(ptr); bar = calculate_bar(s); baz = calculate_baz(s); } Semantically, the bar and baz member variables should be const, since they should not change after initialization. However, it seems that in order to make them so, I would need to initialize them in an initialization list rather than assign them. To be

How to catch the exception in initialization list? [duplicate]

荒凉一梦 提交于 2019-11-30 20:13:18
This question already has an answer here: Catching exceptions from a constructor's initializer list 5 answers I have a question about how to catch the exception in the initialization list. For example, we have a class Foo derived from Bar class Foo { public: Foo(int i) {throw 0; } } class Bar : public Foo{ public: Bar() : Foo(1) {} } I think the syntax is like this (even though it's better to catch such things in the caller. And what are you going to do once you caught it?) Bar::Bar() try : Foo(1) { } catch( const SomeException &e ) { } C++ has a mechanism for doing so, but it is rarely used.

How to catch the exception in initialization list? [duplicate]

独自空忆成欢 提交于 2019-11-30 04:47:44
问题 This question already has answers here : Catching exceptions from a constructor's initializer list (5 answers) Closed 3 years ago . I have a question about how to catch the exception in the initialization list. For example, we have a class Foo derived from Bar class Foo { public: Foo(int i) {throw 0; } } class Bar : public Foo{ public: Bar() : Foo(1) {} } 回答1: I think the syntax is like this (even though it's better to catch such things in the caller. And what are you going to do once you

C++: Initialize a member pointer to null?

梦想的初衷 提交于 2019-11-29 06:06:36
问题 I have a class that looks like: class Foo { public: Foo(); virtual ~Foo(); private: Odp* bar; }; I wish to initialize bar to NULL . Is this the best way to do it? Foo::Foo() : bar(NULL) { } Also, is it necessary that the destructor is virtual? (If that is true, then must the constructor be virtual as well?) 回答1: I wish to initialize bar to NULL . Is this the best way to do it? It is the correct way. So, yes. Also, is it necessary that the destructor is virtual? No. The destructor only needs