This question already has answers here:
Is there a way to make a C++ struct value-initialize all POD member variables?
(3 answers)
Closed 1 year ago.
I have a struct that only has double member variables, e.g.
struct foo { double a, b, c; };
Another class has a std::vector<foo> v member that calls the std::vector<foo>::vector(size_t) constructor in the initializer list.
What I'd like to do is write a default constructor for foo so that all the doubles in it are initialized to zero, without having to write
foo(): a(0), b(0), c(0) { }
I keep needing to add more variables to foo, but it doesn't make sense to have them be elements of a container like std::array<double>, because they all serve distinct purposes.
Since you've tagged this as C++14, you can initialize member variables like this without having to initialize them in a constructor:
struct foo {
double a = 0.0;
double b = 0.0;
double c = 0.0;
};
Related
This question already has answers here:
What is the difference between "::" "." and "->" in c++ [duplicate]
(8 answers)
(->) arrow operator and (.) dot operator , class pointer
(6 answers)
Closed 8 months ago.
Say I have a class with a public variable (bad practice, I know), and in the main function, I want to create 3 class objects, how could I assign different values to that class variable?, something like:
class C{
public:
int foo;
};
int main(){
C co[3];
co[0]->foo = 20;
co[1]->foo = 40;
co[2]->foo = 80;
}
Since you have different value for each object, you need to initialize them one by one.
Fortunately there is a simple way to kind of do it in s single swoop without using individual assignments: Initialize them at definition.
Create a suitable constructor:
class C
{
int foo;
public:
C(int foo)
: foo(foo)
{
}
};
Then you can initialize the array like any other array:
C co[3] = { 20, 40, 80 };
This question already has answers here:
Does the default constructor initialize built-in types?
(7 answers)
What happens when you define an empty default constructor?
(2 answers)
Closed 1 year ago.
Is there any difference regarding the initialization of the x member variable in these cases:
struct A {
int x;
A() {}
};
struct B {
int x;
B() : x(0) {}
};
struct C {
int x;
C() : x() {}
};
For all these cases, in the tests I did, x is always set to the initial value of 0. Is this a guaranteed behavior? Is there any difference in these approaches?
For B::B(), x is direct-initialized as 0 explicitly in member initializer list.
For C::C(), x is value-initialized, as the result zero-initialized as 0 in member initializer list.
On the other hand, A::A() does nothing. Then for objects of type A with automatic and dynamic storage duration, x will be default-initialized to indeterminate value, i.e. not guaranteed to be 0. (Note that static and thread-local objects get zero-initialized.)
This question already has answers here:
Zero-Initialize array member in initialization list
(3 answers)
C++ Initializing Non-Static Member Array
(9 answers)
How to initialize all members of an array to the same value?
(26 answers)
Closed 3 years ago.
Suppose we have a class like the following one:
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}
As you can see we can initialize our private members' aa, bb, cc using the myprogram() constructor.
Now, suppose I have a large private array G_[2000]. how I could initialize all the values of this array equal to 0 using a constructor.
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;
double G_[2000];};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}
Use std::memset function in constructor's body.
For example,
myprogram::myprogram()
: aa{0.0}, bb{0.0}, cc{0.0}
{
std::memset(G_, 0, 2000 * sizeof(double));
}
However, if you use braces {} in your initializer list, it will set default-initialize object (In case of array, it will fill it by zeroes).
You can write:
myprogram::myprogram()
{
for(int i=0;i<2000;i++)
G_[i]=0;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing in constructors, best practice?
I'm new to C++.
Suppose we have this class definition:
Class MyClass {
int a;
int b;
//....
}
I would like to know which is the difference between the two class contructors:
public:
MyClass(int a, int b) : a(a), b(b) {}
and (i would say Java-style):
MyClass(int a, int b) {
this->a = a;
this->b = b;
}
I suppose the first is better in C++; right? why?
The first one (using initializer list) initializes the data members to the given values. The second one initializes them first, then assigns them the values. That is the reason the first is prefered. There is no unnecessary assignment operation there.
This is particularly important when your data members are expensive to construct and/or assign to. Also bear in mind that some types are not default constructable, making it mandatory to use the initializer list.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.