member-initialization Questions

4

Solved

Suppose I want to have a constructor that receives some parameters, and with these parameters I can calculate the values for it's member variables. Except that the values for the member variables a...
Taliesin asked 8/11, 2013 at 3:59

2

Solved

While (re)implementing a simple constexpr map, I wrote this (godbolt): template <class key_type, class value_type, int N> class flat_map { private: struct pair { key_type key; value_type ...
Ciri asked 12/2, 2023 at 18:9

2

Solved

During the member initialisation of a class with multiple members, it seems desirable to be able to catch an exception generated by any specific member initialiser, to wrap in additional context fo...
Modiolus asked 17/7, 2022 at 7:31

1

Solved

class A { int x; static int i; }; int x = 10; int A::i = x; When I compile the code above, it get the error <source>:8:12: error: invalid use of non-static data member 'A::x' 8 | int A:...
Tierza asked 7/2, 2022 at 10:1

4

Solved

Let us consider the following classes struct test1 { int a; int b; test1() : a(0), b(0) {} }; struct test2 { int a; int b; test2() { a = 0; b = 0; } }; Now, I know that test1() constructor ...

1

Solved

code #include<iostream> struct A { private: public: int &p,q; A(int &k1,int k2):p(k1),q(k2) { } }; int main() { int x=2; A a1(x,3); std::cout<<&x<<&quo...
Siege asked 7/7, 2021 at 15:54

8

Solved

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; strin...

2

Solved

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 &...
Implosion asked 20/3, 2018 at 14:34

2

Solved

Brace-or-equal-initializers in an anonymous struct within a struct doesn't do their work on output produced by VS2013. There's the code: #include <iostream> #include <cstdint> struct...

3

Solved

I agree with the consensus that it's generally best to initialise C++ data members in a member initialization list rather than the body of a constructor, but I am sceptical of this explanation T...

1

Solved

#include <cassert> #include <cmath> int main() { struct point_of_cone { double x, y; double z = [&] { using std::sqrt; return sqrt(x * x + y * y); }(); }; point_of_cone p = {...
Acknowledgment asked 18/10, 2016 at 10:59

4

Solved

Consider the following code in which we initialize part of D based on another part of D: struct c { c() : D{rand(), D[0]} {} int D[2]; }; int main() { c C; assert(C.D[0] == C.D[1]); } Is th...
Laaland asked 27/6, 2015 at 17:48

3

Solved

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...
Kaczmarek asked 7/11, 2014 at 15:16

2

Solved

For example, I cannot write this: class A { vector<int> v(12, 1); }; I can only write this: class A { vector<int> v1{ 12, 1 }; vector<int> v2 = vector<int>(12, 1); }; W...
Delphadelphi asked 19/7, 2014 at 3:44

5

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 initiali...
Hawkweed asked 15/9, 2013 at 11:17

2

Solved

For example, I have two classes class Foo; class Bar; class Foo { const Bar &m_bar; ... }; class Bar { const Foo &m_foo; ... }; Let foo is object of Foo and bar is object of Bar. I...

2

Solved

Is it possible use default arguments with member initialization lists? Vector3::Vector3(double xI, double yI, double zI) : x(xI=0), y(yI=0), z(zI=0) { } The constructor always sets x, y, and z t...
Analyzer asked 5/6, 2013 at 0:25

1

Solved

I was recently discussing with a friend and they said there is a performance gain when you use an initialization list to (as opposed to not and simply assigning the data members) when creatin...
Staford asked 15/12, 2012 at 17:20

2

Solved

I have a confusion on class member variable initialization. Suppose in my .h file is: class Test { int int_var_1; float float_var_2; public: Test(); } My .cpp would be: Test::Test() : int_v...

2

Solved

I have seen the peculiar syntax in an SO question a while ago. class B{ A a; public: B() try : a() {} catch(string& s) { cout << &s << " " << s << endl; }; }; ...
Prewitt asked 20/7, 2012 at 9:7

2

Solved

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...
Scallop asked 24/6, 2012 at 0:40

2

Solved

I have a member of class A in my own class which constructor takes multiple parameters. Im forwarding parameters of my own class to the constructor of class A. But its important that these paramete...
Stivers asked 1/1, 2012 at 23:24

6

Solved

Please explain how to use member initializer lists. I have a class declared in a .h file and a .cpp file like this: class Example { private: int m_top; const int m_size; /* ... */ public: Examp...

3

Solved

My gut feeling is it is not. I am in the following situation: class PluginLoader { public: Builder* const p_Builder; Logger* const p_Logger; //Others }; PluginLoader::PluginLoader(Builder* c...

2

I'm writing this copy constructor: //CCtor of RegMatrix RegMatrix::RegMatrix(const RegMatrix &other){ this-> numRow = other.getRow(); this-> numCol = other.getCol(); //Create...
Gramarye asked 10/10, 2010 at 8:19

© 2022 - 2025 — McMap. All rights reserved.