Questions tagged [member-initialization]
121 questions
0
votes
1 answer
Motive : This code aims to provide default values to structure members inside structure defination
Here's my code:
#include
struct parent_structure
{
char pe_1 = 'a';
};
struct child_structure
{
int ce_1 = 1;
int ce_2 = 2;
int ce_3 = 3;
struct sample_structure
{
int ss_1 = 1 ;
char ss_2 = 'a';
…

Coder
- 1
- 1
0
votes
0 answers
Is using a default member initializer with a previously default member-initialized variable legal?
The following code unsurprisingly fails when used:
struct Foo {
std::string b = a + " world"; // a not declared yet
std::string a = "Hello";
};
because we're attempting to use a before having defined it and due to [class.base.init]/13.3:
Then,…

andreee
- 4,459
- 22
- 42
0
votes
0 answers
Packed union of non-pod structs
I want to know, is it acceptable to organize data in following manner? Structures is not POD types anymore, due to member initializers, and compiler gives warning about packed attribute being ignored. Is it possible to make them guaranteed…

Egor.Uttsov
- 45
- 7
0
votes
2 answers
Why we cannot initialize the member variable of a class in C++
It's giving error:
#include
using std::cout;
class stud
{
int a = 0; // error??
public:
void display();
};
int main()
{
// ...
}

Udesh
- 11
- 4
0
votes
1 answer
What Form Does the Implicitly-Declared Default Constructor Take?
So let's say I'm working with this toy example:
struct Foo {
int member;
};
I know that the default constructor won't default initialize member. So if I do this, member remains uninitialized: const Foo implicit_construction. As an aside this…

Jonathan Mee
- 37,899
- 23
- 129
- 288
0
votes
0 answers
Is it possible to initialize an object in a class function, not in constructor
Hey i am looking to see if there is a way for me to initialize a member object in a class function. Meaning i would like not to use the member initializer list.
I don't want:Foo::Foo(): bar(), barBar(){}
but want: Foo::setup(){bar(); barBar() }
Hope…

Neobonde
- 85
- 2
- 7
0
votes
0 answers
member initializer lists in constructor with same-name variables
In this example:
class foo{
public::
foo(int x):x(x){
}
int x;
};
My compiler is able to understand that, in x(x), the inner x is the argument and the outer x is the member variable without writing this->.
Is this…

Humam Helfawi
- 19,566
- 15
- 85
- 160
0
votes
1 answer
unique_ptr can't initialize
I have the following code in a project, and it gives me an error C2059, syntax error "new" that the unique_ptr line is wrong.
#include
class Nothing {
public:
Nothing() { };
};
class IWriter
{
public:
IWriter() {
…

SinisterMJ
- 3,425
- 2
- 33
- 53
0
votes
3 answers
How to initialize array of objects in constructor?
I have class called HighWaterDetector:
class HighWaterDetector {
public:
HighWaterDetector(Device* device);
NCD2Relay ncd2Relay;
// Output output1;
Output outputs[2];
CloudMsgParser cloudMsgParser;
Device * devicePtr;
};
How…

Felix
- 51
- 2
- 10
0
votes
1 answer
Circular dependency with classes that can be initialized one from another
Well, the correct title of this question should be "Circular dependency with classes which instances can be initialized one from another".
I have two classes (Index3i with integer data fields and Index3f with float data fields) that meant to be…

ezpresso
- 7,896
- 13
- 62
- 94
0
votes
2 answers
Member integer changes when directly instantiated from another class
Say I have two classes, A and B. A has an integer, which it displays in the console from its constructor. It also has a member object of B. B displays an integer just like A, but it gets its integer from A when A creates it. The integer must go…
user3152847
0
votes
2 answers
Under what kind of circumstance would a stored property in a structure not have a default value?
I'm new to Swift and is trying to learn the concept of Memberwise Initialisers. The statement below came from "The Swift Programming Language 2.1".
My question is, under what kind of circumstance, a stored property in a structure would not have a…

Thor
- 9,638
- 15
- 62
- 137
0
votes
3 answers
Does member initialization list copy string C++
if I use member initialization list in my constructor of a class that has char* in it, does it copy the value to a new allocated memory or just points the pointer on the same value?
ex.
MyString::MyString(const MyString & other) : m_str(other.m_str)…

Ofek .T.
- 741
- 3
- 10
- 29
0
votes
1 answer
Struggling to understand 'member initializing'
I have been working through C++. I am struggling to get my head round the member initialization, particularly one line of code. Consider the following program;
#include
#include
class RGBA
{
private:
uint8_t m_red = 0;
…

FrankWhite
- 97
- 1
- 3
- 11
0
votes
3 answers
How to write member initialization list with an object from another struct
If I have a binary search tree header file BST.h and inside it I have:
template
struct bstNode
{
type info;
bstNode * lLink;
bstNode * rLink;
};
And then I have an AVL.h header file and I want to use the bstNode struct in…

David Velasquez
- 2,346
- 1
- 26
- 46