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".
78
questions
2
votes
1
answer
88
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.
// ...
2
votes
2
answers
120
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 ...
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 ...
2
votes
1
answer
143
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)
...
2
votes
3
answers
259
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 ...
0
votes
1
answer
629
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>
...
1
vote
1
answer
84
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 ...
8
votes
3
answers
635
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:
...
0
votes
2
answers
45
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 ...
0
votes
3
answers
254
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;
...
2
votes
2
answers
108
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."
...
0
votes
2
answers
245
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 ...
1
vote
1
answer
738
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'า'), // ...
3
votes
2
answers
148
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 ...
0
votes
0
answers
196
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" <<...
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?
};
(...
1
vote
0
answers
90
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 : ...
4
votes
4
answers
187
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:
...
1
vote
1
answer
197
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() = ...
2
votes
2
answers
242
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 : ...
5
votes
2
answers
244
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 ...
1
vote
1
answer
806
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 ...
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) {}
...
1
vote
0
answers
69
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 ...
4
votes
1
answer
722
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 : ...
4
votes
3
answers
582
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 '-...
1
vote
2
answers
443
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 ...
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:
...
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 ...
27
votes
2
answers
3k
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.
#...
1
vote
1
answer
527
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 ...
28
votes
5
answers
10k
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
{
...
2
votes
2
answers
468
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 ...
1
vote
5
answers
89
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 ...
-2
votes
2
answers
335
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 ...
1
vote
1
answer
606
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 ...
2
votes
2
answers
737
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(...
4
votes
3
answers
576
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 ...
2
votes
1
answer
544
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>
...
0
votes
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 ...
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;
};
...
-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
{
...
0
votes
2
answers
258
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 ...
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;
...
0
votes
1
answer
538
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} ;
...
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 ...
122
votes
1
answer
49k
views
Member initialization while using delegated constructor
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 trying ...
37
votes
2
answers
21k
views
Can member functions be used to initialize data members in the member initializer list?
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 ...
25
votes
5
answers
58k
views
Initializing a reference to member to NULL in C++
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() :...
31
votes
6
answers
73k
views
Is a member initializer list part of the declaration or the definition of a constructor?
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:
...