Questions tagged [member-initialization]

121 questions
0
votes
1 answer

Member initialization syntax in C++ constructors

Why does the following not compile? class A { public: A(int a) : a_{a} {} private: int a_; };
ethelion
  • 105
  • 6
0
votes
1 answer

cppcheck: member variable not initialized in the constructor

The following code, as far as I can tell, correctly initializes the variables of the derived class B: #include struct A { int i; }; struct B : A { int j; explicit B(A&& a) : A(std::move(a)), j{i} { } }; int main() { A a{3}; B…
francesco
  • 7,189
  • 7
  • 22
  • 49
0
votes
1 answer

Why does a Member initialized list for a default constructor in a composite class not call the member object constructor?

The Member initialized list for a default constructor in a composite class does not call the member object constructor. #include struct test{ test(){ std::cout << "defualt is called" << std::endl; } test(int num){ …
0
votes
1 answer

Call different initializer after type checking

I have a generic class, where two of the generics are going to be identical 90% of the time, but on the off chance that they are separate types, I need to perform member initialization a little bit differently. template
Oren Bell
  • 460
  • 1
  • 5
  • 13
0
votes
1 answer

Initialize member in class constructor and pass member down to base class at the same time

I have a main class (MyClass) derived from MyBase. Also I have a Helper class (used by MyClass) derived from HelperBase (used by MyBase). Is there a way to initialize the Helper in the constructor initialization of MyClass and also pass the helper…
0
votes
0 answers

Direct list initialization or direct initialization in member initializer list?

I recently caught up on Member Initializer Lists. The Cpp-Learning-Site suggests to always prefer direct list initialization over direct initilization. int a(5); int b{5}; // preferred I noticed that both of these can be used in Member…
Raumschifffan
  • 360
  • 2
  • 13
0
votes
1 answer

Is a member initialization list without a value defined?

Say I have : class Foo { public: int x; Foo() : x() {} } Would it be UB to read x after the constructor has ran? More specifically, what type of initialization is this, zero, direct or default initialization? I know if instead we'd have: Foo()…
0
votes
1 answer

Problem while initializing reference variable of class through Member initialization list

code #include 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<<"\n"; // …
Abhishek Mane
  • 619
  • 7
  • 20
0
votes
0 answers

A choice of constructor arguments in a member initializer list

This question may turn out to be more about CPUs/memory than the C++ language itself. I have a class B provided below. class B { public: B(int num): num_(num), a(num_){} private: int num_; A a; }; In the constructor of B, the object a…
mana
  • 545
  • 4
  • 12
0
votes
0 answers

Member Initialization of a constructor in C++

I want to ask you why those two 'Student' constructor commented below don't work. #include #include using namespace std; class Temp{ public: void Tempshow(){ cout << "Mother Class"…
Shrew
  • 13
  • 2
0
votes
2 answers

How to fix of [-Wreorder] & [-Wuninitialized] warrings when initializing using member-initializer list?

In a class that looks somehow like this: MyClass.h class MyClass{ public: MyClass(); int x1; int x2; int x3; int x4; int x5; private: int y1; int y2; }; MyClass.cpp #include "MyClass.h" int someFunc1(int x1, int…
Bobo Feugo
  • 162
  • 1
  • 10
0
votes
2 answers

Query about member initialization

while I was doing hackerrank c++ exercises I stumbled upon this code in the discussions section: class BadLengthException : public std::runtime_error { public: BadLengthException(int length) : std::runtime_error{std::to_string(length)} {…
Aero
  • 3
  • 2
0
votes
0 answers

class 'Ninja' does not have any field named 'Damage'

Base class: class Entity { public: // friend // e1 attacker, e2 target friend void Attack( Entity &e1, Entity &e2 ); // methods set attributes virtual void setDamage ( int x ) { this -> Damage = x; } virtual void…
0
votes
2 answers

Implementation file (.cpp) for a class with member initialization

My question must be simple, but I cannot find a right way to split the constructor with initialized members to .h and .cpp (definition and implementation), files. If, say, I have: class Class { public: Class (int num, float fl ... ) :…
RockOGOlic
  • 150
  • 1
  • 7
0
votes
2 answers

Initializing a member in an inherited class with different value of base class

Lets say I have an Entity class with the variable x and it is defined as 0 in that class. Well then I make a derived class Player but I want the Player's inherited x by default to be 1 and not 0, so that every Player I create has x be 1 by default.…
Aravash
  • 39
  • 3
1 2 3
8 9