initialization-list

C++ Constructor initialization list strangeness

喜你入骨 提交于 2019-12-21 08:26:32
问题 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

Why doesn't Java have initializer lists like in C++?

和自甴很熟 提交于 2019-12-18 10:03:41
问题 In C++, you can use an initializer list to initialize the class's fields before the constructor begins running. For example: Foo::Foo(string s, double d, int n) : name(s), weight(d), age(n) { // Empty; already handled! } I am curious why Java does not have a similar feature. According to Core Java: Volume 1 : C++ uses this special syntax to call field constructors. In Java, there is no need for it because objects have no subobjects, only pointers to other objects. Here are my questions: What

Class member without a default constructor

…衆ロ難τιáo~ 提交于 2019-12-18 08:32:06
问题 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

How do I initialize a stl vector of objects who themselves have non-trivial constructors?

蹲街弑〆低调 提交于 2019-12-17 17:32:25
问题 suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int n) : n_(n) {}; // MORE STUFF }; And suppose this class don't have a default trivial constructor MyInteger() . I must always supply an int to initialize it for some reason. And then suppose that somewhere in my code I need a vector<MyInteger> . How do I initialize each MyInteger component in this vector<> ? I have two situations (probably the solution is the same, but I'll state them anyway), a normal

Initialize const members using complex function in C++ class

夙愿已清 提交于 2019-12-11 02:55:00
问题 I have a program that works with 3d grids. This grid has its own class object Grid that looks like this (simplified version): class Grid { public: Grid() { readDataFromInputFile(); } private: void readDataFromInputFile() {...} // this function reads the values for i, j, k from disk int i; int j; int k; }; What I would like to do now, is to be able to set the variables i, j and k as const int, to avoid messing them accidentally up in the other functions. I can however not easily set them in a

Circular dependency in constructor initialization list

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:24:11
问题 Is the following well-defined? class A; class B; // define A, which takes B& in constructor // define B, which takes A& in constructor class C { A a; B b; public: C() : a(b), b(a) { /* stuff with a and b */ } } Full example at ideone.com. Is it safe/well-defined so long as the constructors for A and B don't do anything with the references they get? 回答1: N4140 [class.cdtor]/1 reads: For an object with a non-trivial constructor, referring to any non-static member or base class of the object

Zero-Initialize array member in initialization list

偶尔善良 提交于 2019-12-09 04:33:57
问题 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

How do I make a deep copy in a constructors initialization list. c++

最后都变了- 提交于 2019-12-08 06:42:33
问题 This is the constructor for node in list class. I need to make a deep copy of winery, an other class in the initialization list. Item is an instance of winery. List::Node::Node(const Winery& winery) : item(winery) // your initialization list here { Winery w = winery; item = w; } constructor for winery: Winery::Winery(const char * const name, const char * const location, const int acres, const int rating) : name(name), location(location), acres(acres), rating(rating) { char *nm = new char

Initializing vector<string> with double curly braces

邮差的信 提交于 2019-12-07 10:39:01
问题 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 回答1: Implicit conversion central. That's what's going

C++ initialization lists for multiple variables

怎甘沉沦 提交于 2019-12-07 01:02:39
问题 I'm trying to learn how to initialize lists. I have a simple class below and trying to initialize the list of variables. The first Month(int m): month(m) works. I'm trying to do something similar below that line with more than one variable. Is this possible in that format? would I have to break away from the one liner? class Month { public: Month(int m) : month(m) {} //this works Month(char first, char second, char third) : first(first){} : second(second){} : third(third){} //DOES NOT WORK