How are we doing? Please help us improve Stack Overflow. Take our short survey

Questions tagged [ctor-initializer]

A member initializer list in C++ is part of the constructor definition, and responsible for initializing subobjects. "ctor-initializer" refers to a rule in the C++ grammar which expands to ": mem-initializer-list".

ctor-initializer
Filter by
Sorted by
Tagged with
1 vote
2 answers
92 views

Structured binding in constructor initialization list?

This is similar in vein to Is it possible to use structured binding in the member initializer list of a constructor?, except that in my case, it is not a dislike-versus-like scenario rather it is a ...
Happy Green Kid Naps's user avatar
2 votes
1 answer
133 views

How to perform complex initialization of a const data member in the member initializer list

Is there a way in modern C++ to initialize a const value in a class derived from accessing a pointer argument? and that pointer might be null, so basically after doing a check on the pointer? e.g. // ...
Raffaello's user avatar
  • 1,675
2 votes
2 answers
169 views

C++ class initializer list in library causes stack corruption

I've distilled my problem down to a (hopefully) very simple example. At a high level, I have a shared library which provides a class implementation, and a main executable which uses the library. In my ...
Paul Grinberg's user avatar
0 votes
1 answer
58 views

Is an array in a member initializer list for a variadic struct possible?

I created a variadic struct based on a std::tuple. Then, I would like to use a C-style array in the member initializer list with automatic type deduction / class template argument deduction. I created ...
A M's user avatar
  • 15k
2 votes
1 answer
161 views

C double parentheses

I don't know what this "feature" is called so I coudn't google it also I'm sorry if the title doesn't make sense. I recently looked at suckless dwm's source and saw this code: (from dwm.c) ...
Zer0day's user avatar
  • 89
2 votes
3 answers
296 views

Is member initializer list considered part of the body of a constructor or it it considered part of the declarator

I am learning about member initializer lists in C++. So consider the following example: struct Person { public: Person(int pAge): age(pAge) // ^^^^^^^^^ is this ...
user12002570's user avatar
  • 44.1k
1 vote
1 answer
874 views

Member initialization while using delegate constructor

The C++ standard does not allow delegate constructors and member initializers in a single mem-initializer-list, yet the following code compiles fine with clang++ and g++. #include <iostream> ...
Jorge's user avatar
  • 119
1 vote
1 answer
98 views

initializing reference variable of class object with member initializer list?

https://stackoverflow.com/a/8523361/11862989 from this answer this question is came into picture. I feel small thing is wrong in that answer, so I commented out there but no reply from him (guy (190K ...
user avatar
8 votes
3 answers
689 views

Why do the constructor of the derived classes want to initialize the virtual base class in C++?

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: ...
scozy's user avatar
  • 2,541
0 votes
2 answers
48 views

When the entire stack is displayed, only the last elements of the stack are displayed. Why?

I need to add new items to the end of the list, delete the last one and display the entire list. When displaying the entire list, for some reason, only the last elements of the stack are displayed, by ...
Constantine Lebedev's user avatar
0 votes
3 answers
265 views

Default constructor is being called with no reference to it

why the following code won't work: #include <iostream> class Entity { public: /* Entity() { std::cout << "Create Entity with default constructor" << std::endl; ...
user avatar
2 votes
2 answers
118 views

Problem while initializing attribute in constructor c++

While i try to debug ERROR appear : "Unhandled exception at 0x5784F2F6 (ucrtbased.dll) in Final project.exe: An invalid parameter was passed to a function that considers invalid parameters fatal." ...
Yassine Mrabet's user avatar
0 votes
2 answers
278 views

Error while declaring constructor in an Inherited class in c++

I have been given two classes, Person and Student, where Person is the base class and Student is the derived class. Any change in Person class or main function is not allowed.. Observe that Student ...
Ankit_jackar's user avatar
1 vote
1 answer
829 views

C++ constructor initializer list " error: expected '(' or '{'"

This doesn't compile: class foo { struct node { wchar_t val; unordered_map<wchar_t,unique_ptr<node>> children; }; node root; public: foo() : root.val(L'า'), // ...
user avatar
3 votes
2 answers
158 views

How can I call two functions in contructor's member initializer list?

Can i call a function in initialize list? please see this code: #include <string> using namespace std; class A { public: A(string path) : s(cfg.getRoot()) { // before i call getRoot, i ...
nick huang's user avatar
0 votes
0 answers
205 views

Understanding how to initialize constructors in the Multiple Inheritance

I having hard time in solving those kind of question. In an exam I going to take in a few days, they show a program in C++ which has Multiple Inheritance: struct X { X(){cout << "X" <<...
vesii's user avatar
  • 2,980
8 votes
4 answers
556 views

Why can't I use =default for default ctors with a member initializer list

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? }; (...
andreee's user avatar
  • 4,589
1 vote
0 answers
95 views

Make virtual class use base class of another class

I'm currently trying to build a hierarchy of classes that uses multiple inheritance. I have classes A, B, C and D, related as such: struct A { int a; A(int a_) : a(a_) {} }; struct B : ...
Filipe Rodrigues's user avatar
4 votes
4 answers
190 views

Constructor Definition C++ syntax [duplicate]

What's the difference between these two declarations of constructors: class Fruit { private: int price; public: Fruit(int x): price(x) { } }; VS class Fruit { private: ...
pseudo_teetotaler's user avatar
1 vote
1 answer
200 views

C++ Multi-level virtual Inheritance compilation problem

I am working on a C++ project wherein I am making use of multi-level inheritance. This is my sample code: class ClassA { public: ClassA(int a,int b) : sum(a + b) {} virtual ~ClassA() = ...
Harry's user avatar
  • 2,395
2 votes
2 answers
243 views

virtual base classes initiailization

I am working on a test and I have hard time to understand this one: #include <iostream> struct Car { Car() : price(20000) {} Car(double b) : price(b*1.1) {} double price; }; struct Toyota : ...
fabrice's user avatar
  • 125
6 votes
3 answers
313 views

Is taking the address of a member of an uninitialized object well defined?

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. struct ...
François Andrieux's user avatar
2 votes
1 answer
913 views

Is it possible to initialize a member variable in the constructor body, instead of the initializer list? [closed]

As you might know, A::A() { this->foo = 1; } Is the same as: A::A() : foo(1) { this->foo = 1; } Which is inefficient because of the double declaration. The compiler might optimize ...
user3535030's user avatar
24 votes
2 answers
1k views

Does incrementing in a member initializer list generate undefined behavior?

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) {} ...
Keerpich's user avatar
  • 341
1 vote
0 answers
70 views

Using C++ construction initializer to open/read file

I wrote a simple C++ class program to read and/or to print out some BMP header information as shown below and it works just fine. However, I want to take the advantage of what a C++ contructor ...
Habibie's user avatar
  • 11
4 votes
1 answer
778 views

Internal mechanism of virtual inheritance

Sample code in C++: class A { public: A(int) {} }; class B : public virtual A { public: B(int b) : A(b) {} }; class C : virtual public A { public: C(int c) : A(c) {} }; class D : ...
paper.plane's user avatar
  • 1,199
5 votes
3 answers
627 views

why are virtual base non-default constructors not called unless most-derived base explicitly invokes them?

I would like to understand WHY C++ standard mandates that virtual base non-default constructors cannot be invoked by an intermediate NOT most-derived class, as in this code, when compiled with '-...
JVD's user avatar
  • 655
1 vote
2 answers
451 views

Why default constructor of most base class (Virtual) is not getting called in private virtual inheritance while creating object of most derived class?

How default constructor of most base class is getting called in private virtual inheritance while creating object of most derived class. But the same does not get called when mentioned in constructor ...
Gaurav's user avatar
  • 171
7 votes
4 answers
3k views

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

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: ...
Gaurav's user avatar
  • 171
0 votes
2 answers
49 views

Avoid double-calling function in string assignment in ctor initializer list

In the following code example, I want to initialize std::string A::str_ in A's initializer list with either the return value from a function (that may return NULL), or a const char*. But I don't like ...
StoneThrow's user avatar
  • 5,943
28 votes
2 answers
4k views

The 'this' pointer in the initialization list of the constructor

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 list. #...
Hemant Bhargava's user avatar
1 vote
1 answer
541 views

Deriving class from virtual base with no default constructor

I'm writing a small hierarchy of exception classes for a C++ application I'm developing, and I'm having trouble deriving indirectly from std::runtime_error. Here is code analogous to what I've written ...
C. Finegan's user avatar
29 votes
5 answers
11k views

What's the differences between member initializer list and default member initializer on non-static data member?

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 { ...
markzzz's user avatar
  • 47.7k
2 votes
2 answers
474 views

Constructor with an empty mem-initializer-list and an empty body

Where can I find in the Standard (C++14) a clause stating that an user-defined default constructor with an empty mem-initializer-list and an empty body invokes the default constructor for each base ...
François-Marie Arouet's user avatar
1 vote
5 answers
90 views

in C++, how should I enforce the order of composited object constructors in an initializer list when they have dependencies

If I have a class that composit other objects that have inter-dependencies, (how) should I enforce their construction order? E.g. class Parent { Child1 c1; Child2 c2; }; Imagine the ...
nappyfalcon's user avatar
-2 votes
2 answers
338 views

C++ Explain "one liner" constructor that overloads member function Iterator(int i = 0) : i(i) { };

I have a question about what this contractor is actually doing. I found it online and it works for my purpose, but I wish to understand its notation. class Iterator { int i; public: Iterator(int ...
user3822637's user avatar
1 vote
1 answer
620 views

C++ initialization list in constructor

I'm trying to initialize an instance of a class called "Winery" using an initialization list in the constructor for another class called "List." The problem is that when I hand the ...
user3698112's user avatar
2 votes
2 answers
749 views

Initialising base classes when using virtual inheritance

I'm getting unexpected errors when compiling the following code using Xcode 5.1 on OS X. Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) class GrandParent { public: GrandParent(...
ksl's user avatar
  • 4,629
4 votes
3 answers
599 views

initializing const fields in the constructor

const fields in C++ must be initialized in the initialization list, this makes non trivial the computation of interdependent values from the constructor parameters. What is(are) the best way(s) to ...
factoriel's user avatar
2 votes
1 answer
548 views

May I call a virtual function to initialize a base-class sub-object?

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> ...
prestokeys's user avatar
  • 4,825
1 vote
3 answers
2k views

Initializing a unique_ptr in constructor of base class properly

I try to pass an std::unique_ptr to an inherited class, which will forward it to the base class constructor (using an constructor initializer list). If the base class constructor receives an nullptr ...
Constantin's user avatar
  • 8,809
5 votes
3 answers
2k views

Populating std::array in class' constructor initializer

Is there a way to populate a std::array in a class' constructor initializer? Right now I'm populating it the long way: class Matrix3x3 { //... private: std::array<double, 9> _indicies; }; ...
Casey's user avatar
  • 10.7k
-2 votes
3 answers
4k views

Which constructor is called first? [duplicate]

I have looked at similar queries but I seem to be getting myself lost. I have a simple example, so please consider the following: #include <iostream> using namespace std; class Animal { ...
Metal Paw's user avatar
  • 112
35 votes
4 answers
21k views

What is the difference between initialization and assignment in a constructor?

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(...
Uttam Malakar's user avatar
0 votes
2 answers
265 views

Preventing construction by throwing exception before constructor body

C++ I want a class to throw an exception before its constructor's body's opening curly brace { by using its own member function to prevent construction. I defined a member function, whose purpose is ...
CodeBricks's user avatar
  • 1,791
2 votes
3 answers
1k views

Error while calling constructor from another constructor in C++

I have a struct A that has several constructors with different data members initialized. template<typename T> struct A { typedef std::vector<T> type1 type1 a; type1 b; ...
user592748's user avatar
  • 1,214
0 votes
1 answer
574 views

Add DataCollection properties while Initialize Objects by Using an Object Initializer

Add properties while Initialize Objects by Using an Object Initializer . But how can we add DataCollection property? Example: class Student{ public string FirstName{ get; set} ; ...
SP007's user avatar
  • 1,901
5 votes
1 answer
3k views

Is there a performance difference between member initializer lists and assignment? [duplicate]

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 creating objects ...
user997112's user avatar
  • 29.9k
3 votes
1 answer
523 views

Would a member initializer, or assignment in the constructor initialize a member faster? [duplicate]

class A { public: int a; A(int x) { a = x; } }; OR class B { public: int b; B(int x):b(x){} }; Which one would initialize the object faster ? Or will same ...
h4ck3d's user avatar
  • 6,234
0 votes
6 answers
102 views

Clean way of optionally including members in C++ ctor-initializer

If I have: Class MyClass { public: MyClass( /* args */ ); private: someType member0; someType member1; // ... someType memberN; #if defined(FIRST_COMPILE_CONDITION) someType ...
shroudednight's user avatar