ctor-initializer Questions

7

Solved

Imagine I have a C++ class Foo and a class Bar which has to be created with a constructor in which a Foo pointer is passed, and this pointer is meant to remain immutable in the Bar instance lifecyc...
Jemappes asked 14/9, 2009 at 20:22

3

Solved

Consider the following example. When bar is constructed, it gives it's base type (foo) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struc...

14

Solved

Recently I've seen an example like the following: #include <iostream> class Foo { public: int bar; Foo(int num): bar(num) {}; }; int main(void) { std::cout << Foo(42).bar << ...
Stimulant asked 10/11, 2009 at 23:29

7

Solved

class C { public: C() : arr({1,2,3}) //doesn't compile {} /* C() : arr{1,2,3} //doesn't compile either {} */ private: int arr[3]; }; I believe the reason is that arrays can be initialized on...

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

6

Solved

Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main(...

1

https://mcmap.net/q/80502/-what-is-this-weird-colon-member-quot-quot-syntax-in-the-constructor from this answer this question is came into picture. I feel small thing is wrong in that answer, so I ...
Marcie asked 20/5, 2021 at 11:22

3

Solved

My understanding, for instance reading this, is that the constructor of a derived class does not call its virtual base class' constructor. Here is a simple example I made: class A { protected: A(...
Tressa asked 3/11, 2020 at 2:6

4

Solved

Consider the following class: class Foo { int a, b; public: Foo() : a{1}, b{2} {} // Default ctor with member initializer list //Foo() : a{1}, b{2} = default; // Does not work but why? }; (Ed...

2

Solved

Is this causing undefined behaviour? Specifically, the incrementing in the initializer list and how will that be evaluated. class Wrinkle { public: Wrinkle(int i) : a(++i), b(++i), x(++i) {} priv...
Anette asked 19/1, 2018 at 12:41

4

Consider the below code: #include<iostream> using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {...

2

I guess I am unable to understand why this is not working. I always thought that I can use 'this' pointer inside the constructor, but I never knew that I cannot use 'this' in the initialization lis...
Illgotten asked 12/1, 2017 at 14:19

5

Solved

I'd like to understand what's the differences of using one form rather than the other (if any). Code 1 (init directly on variables): #include <iostream> using namespace std; class Test {...
Cosmopolitan asked 13/4, 2016 at 13:38

8

Solved

Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1; const double ImportantConstant2; const double ImportantConstant3; private:...
Blow asked 15/7, 2010 at 13:56

3

Solved

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 (s...
Homecoming asked 9/12, 2014 at 15:1

1

Solved

I know that virtual functions should not be called either directly or indirectly in a constructor, but this code runs fine. Is what I have here safe? #include <iostream> #include <string&...
Penn asked 9/9, 2014 at 22:56

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

5

Solved

Is it possible to initialize a reference member to NULL in c++? I'm trying to something like this: class BigClass { private: Object m_inner; public: const Object& ReadOnly; BigClass() : Read...
Globigerina asked 29/1, 2012 at 18:56

1

Solved

I've started trying out the C++11 standard and i found this question which describes how to call your ctor from another ctor in the same class to avoid having a init method or the like. Now i'm try...
Caveat asked 30/8, 2012 at 4:46

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

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

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

Suppose I have: // MyClass.h class MyClass { public: MyClass(); private: Something *something_; } // MyClass.cpp MyClass::MyClass() { something_ = new Something(); } Should I initialize s...
Emanuel asked 27/9, 2011 at 20:47

3

Solved

Is this behavior well-defined? class Foo { int A, B; public: Foo(int Bar): B(Bar), A(B + 123) { } }; int main() { Foo MyFoo(0); return 0; }

6

Solved

I have a class B that requires an instance of class A to be constructed: class B { B(A* a); // there is no default constructor }; Now I want to create a class that contains B as a member, so I ...

© 2022 - 2024 — McMap. All rights reserved.