Questions tagged [member-initialization]
121 questions
1
vote
1 answer
What does it mean by member initialization list initializing an attribute of a class
If I have a class like the below one:
class foo {
private:
int a;
public:
foo (int arg1) : a(arg1) {}
};
The attribute a will be default-initialized in the line it got declared.
Now, when the parameterized constructor is called, it gets…

PhiloRobotist
- 177
- 9
1
vote
1 answer
Pass lambda that captures "this" to obj in member initializer list
I have the following problem: I define a class task that lives within another class object. I want to save a closure inside that task class to invoke it later. Now how excactly am I supposed to initialize the task object within the container class?…

glades
- 3,778
- 1
- 12
- 34
1
vote
1 answer
How to initialize C-style char array and int array via member initialization list through constructor?
Code-1
#include
#include
class A
{
private:
int p[5];
char str[20];
public:
A(int *q, char *s)
{
for(int i=0; i<=4; i++)
{
p[i]=*q;
q++;
}
strcpy(str,s);
…

Abhishek Mane
- 619
- 7
- 20
1
vote
3 answers
Why is accessing the members of a member object not allowed in a ctor?
class Edge:
class Edge {
int dist = 0;
std::pair ends;
public:
Edge() = default;
explicit Edge(const int idist) : dist(idist) { }
explicit Edge(const int idist, Node& end1, Node& end2) : dist(idist) {
…

kesarling He-Him
- 1,944
- 3
- 14
- 39
1
vote
2 answers
How to initialize multiple constant member variables that shares complex initialization code?
Introduction
Let's introduce this simple example:
#include
class X
{
public: // Members
/// A ^ B + A
int A;
/// A ^ B + B
int B;
public: // Specials
X(
const int & A,
const int & B
)
:…
user1180790
1
vote
2 answers
Member initialization list vs assignment/copy constructor (in boost deadline_timer)
I have the following class declaration:
class A {
public:
A();
private:
boost::asio::io_service io;
boost::asio::deadline_timer t;
};
The following constructor for class A works fine:
A::A() : t(io) {
// do stuff
}
But when I write…

nishantsingh
- 4,537
- 5
- 25
- 51
1
vote
2 answers
How do member Initializers work in Structs ? (In Swift)
Since I am a complete beginner, I don't even know whether I am asking the right question or not. But I am having trouble understand why the "User(name: "John ...)" part of the code in the for loop works
CASE1
struct User
{
…

209135
- 517
- 5
- 15
1
vote
2 answers
Should Qt class data members of a C++ class be initialized before being used?
There are many C++ programming best practices that are proposed in many articles and books. Following is a subset that is related to C++ class data members:
Make sure that objects are initialized before they are used.
Make sure that all…

jonathanzh
- 1,346
- 15
- 21
1
vote
2 answers
c++11 in-class member initialization with this
I have a quick question in gcc 4.8 with the flag -std=c++11 enabled.
I can do this and it works fine.
class Test;
class StupidClass {
public:
StupidClass(Test *test) {}
};
class Test {
StupidClass c = StupidClass(/*this is the part in…

user2445735
- 158
- 1
- 5
1
vote
3 answers
Class instance inside class declaration with constant arguments
I am having trouble declaring instances of a class with constant arguments inside the definition of another class.
class Foo
{
private:
const int m_a, m_b;
public:
Foo(int a, int b) : m_a(a), m_b(b) {}
};
class Bar
{
…

Wilsonator
- 407
- 1
- 5
- 14
0
votes
2 answers
Java instance member initialization throws an exception
Let us say I have the following class
public class A {
private B b;
}
Now there is a factory for creating instances of B, but the creator method throws an exception
public class BCreatorFactory {
public static createB() throws SomeException…

Yaneeve
- 4,751
- 10
- 49
- 87
0
votes
0 answers
What syntax should i use for member initializers?
What syntax should i use for member initializers in c++?
I know how to use it for Attribute but not for functions.
#include
using namespace std;
class MyClass {
public:
MyClass(int a, int b,int c):
regVar(a),…

Marco
- 1
- 1
0
votes
0 answers
CTAD for class members in member initializer list?
I wanted to see if I can deduce the template parameters of a member class object in the member initializer list. I suppose this is not (yet) possible. Am I right in assuming so?
Demo
#include
template
struct…

glades
- 3,778
- 1
- 12
- 34
0
votes
1 answer
Compiler error on member initialization of c-style array member (compared with std::array member)
I encountered this problem while initializing a member array variable (c-style). Interestingly converting the member into a std::array<> solves the problem.
See below:
struct A {
A(int aa) : a(aa) {}
A(const A &a) = delete;
A &operator=(const…

avikpram
- 47
- 5
0
votes
0 answers
Is it possible to combine default member initialization and delegating constructors?
Suppose I have a class
class Widget
{
public:
explicit Widget();
explicit Widget(int foo, int bar);
private:
Gadget * gadget_;
int foo_{42};
int bar_{13};
}
with constructors that use some members (therefore initialization…

Jan Hošek
- 187
- 1
- 9