initialization-list

Valgrind Error: in use at exit: 72,704 bytes C++ Initialization List weirdness with char*

穿精又带淫゛_ 提交于 2019-11-29 04:25:37
Issue: I have a weird issue that I wasn't expecting. I have a class called Answers and within the header is this: class Answer { char* aText; bool b_correct; public: Answer():aText(0){;} //default constructor } The main (testing) driver code is this: int main(void) { static const unsigned int MAX_ANSWERS = 5; Answer answers[MAX_ANSWERS]; } The (unexpected) weirdness I am getting is that there is an alloc happening, and I haven't used a new anywhere in my code yet. I'm guessing that the char* is calling this in the initialization list. I am using valgrind to test my code, and I'm getting 11

can member functions be used to initialize member variables in an initialization list?

雨燕双飞 提交于 2019-11-28 17:50:19
OK, member variables can be used to initialize other member variables in an initialization list (with care taken about the initialization order etc). What about member functions? To be specific, is this snippet legal according to the C++ standard? struct foo{ foo(const size_t N) : N_(N), arr_(fill_arr(N)) { //arr_ = fill_arr(N); // or should I fall back to this one? } std::vector<double> fill_arr(const size_t N){ std::vector<double> arr(N); // fill in the vector somehow return arr; } size_t N_; std::vector<double> arr_; // other stuff }; Yes, your use of member function in initialization list

Valgrind Error: in use at exit: 72,704 bytes C++ Initialization List weirdness with char*

若如初见. 提交于 2019-11-27 18:25:44
问题 Issue: I have a weird issue that I wasn't expecting. I have a class called Answers and within the header is this: class Answer { char* aText; bool b_correct; public: Answer():aText(0){;} //default constructor } The main (testing) driver code is this: int main(void) { static const unsigned int MAX_ANSWERS = 5; Answer answers[MAX_ANSWERS]; } The (unexpected) weirdness I am getting is that there is an alloc happening, and I haven't used a new anywhere in my code yet. I'm guessing that the char*

Order of calling base class constructor from derived class initialization list

爱⌒轻易说出口 提交于 2019-11-27 14:47:16
struct B { int b1, b2; B(int, int); }; struct D : B { int d1, d2; // which is technically better ? D (int i, int j, int k, int l) : B(i,j), d1(k), d2(l) {} // 1st Base // or D (int i, int j, int k, int l) : d1(k), d2(l), B(i,j) {} // last Base }; Above is just pseudo code. In actual I wanted to know that does the order of calling base constructor matter ? Are there any bad behaviors (especially corner cases ) caused by any of the cases ? My question is on more technical aspect and not on coding styles. AnT The order you refer in your question is not the "order of calling base constructor". In

Can member variables be used to initialize other members in an initialization list?

半世苍凉 提交于 2019-11-27 12:05:06
Consider the following (simplified) situation: class Foo { private: int evenA; int evenB; int evenSum; public: Foo(int a, int b) : evenA(a-(a%2)), evenB(b-(b%2)), evenSum(evenA+evenB) { } }; When i instanciate Foo like this: Foo foo(1,3); then evenA is 0, evenB is 2, but will evenSum be initialized to 2? I tried this on my current platform (iOS) and it seems to work, but I'm not sure whether this code is portable. Thanks for your help! This is well-defined and portable, 1 but it's potentially error-prone. Members are initialized in the order they're declared in the class body, not the order

can member functions be used to initialize member variables in an initialization list?

て烟熏妆下的殇ゞ 提交于 2019-11-27 10:46:55
问题 OK, member variables can be used to initialize other member variables in an initialization list (with care taken about the initialization order etc). What about member functions? To be specific, is this snippet legal according to the C++ standard? struct foo{ foo(const size_t N) : N_(N), arr_(fill_arr(N)) { //arr_ = fill_arr(N); // or should I fall back to this one? } std::vector<double> fill_arr(const size_t N){ std::vector<double> arr(N); // fill in the vector somehow return arr; } size_t N

C++: Initialization of inherited field

☆樱花仙子☆ 提交于 2019-11-27 09:24:40
I've a question about initialization of inherited members in constructor of derived class. Example code: class A { public: int m_int; }; class B: public A { public: B():m_int(0){} }; This code gives me the following output: In constructor 'B::B()': Line 10: error: class 'B' does not have any field named 'm_int' (see http://codepad.org/tn1weFFP ) I'm guessing why this happens? m_int should be member of B , and parent class A should already be initialized when initialization of m_int in B happens (because parent constructors run before member initialization of inherited class). Where is a

Initialize parent's protected members with initialization list (C++)

半城伤御伤魂 提交于 2019-11-26 18:26:28
Is it possible to use the initialization list of a child class' constructor to initialize data members declared as protected in the parent class? I can't get it to work. I can work around it, but it would be nice if I didn't have to. Some sample code: class Parent { protected: std::string something; }; class Child : public Parent { private: Child() : something("Hello, World!") { } }; When I try this, the compiler tells me: "class 'Child' does not have any field named 'something'". Is something like this possible? If so, what is the syntax? Many thanks! It is not possible in the way you

C++: Initialization of inherited field

折月煮酒 提交于 2019-11-26 17:49:52
问题 I've a question about initialization of inherited members in constructor of derived class. Example code: class A { public: int m_int; }; class B: public A { public: B():m_int(0){} }; This code gives me the following output: In constructor 'B::B()': Line 10: error: class 'B' does not have any field named 'm_int' (see http://codepad.org/tn1weFFP) I'm guessing why this happens? m_int should be member of B , and parent class A should already be initialized when initialization of m_int in B

Can member variables be used to initialize other members in an initialization list?

◇◆丶佛笑我妖孽 提交于 2019-11-26 13:07:20
问题 Consider the following (simplified) situation: class Foo { private: int evenA; int evenB; int evenSum; public: Foo(int a, int b) : evenA(a-(a%2)), evenB(b-(b%2)), evenSum(evenA+evenB) { } }; When i instanciate Foo like this: Foo foo(1,3); then evenA is 0, evenB is 2, but will evenSum be initialized to 2? I tried this on my current platform (iOS) and it seems to work, but I\'m not sure whether this code is portable. Thanks for your help! 回答1: This is well-defined and portable, 1 but it's