Put a parenthesis after member variable to initialize? - c++

I have seen people put a parenthesis after the member variable in the initialization list. I wonder why would people do that?
For example, I have a STL container in header file:
class A{
public:
A();
...
private:
vector<string> v;
}
and in source file:
A::A() : v() {}
My question is what is v() and why do people do that since that doesn't look like v is initialized into a value either

That will run the default constructor or initializer (for plain types) for the member. In this context, it will default construct the vector. Since it is the default constructor, it is not necessary here. v would have been default constructed in the absence of an initializer.
class Example {
private:
int defaultInt;
vector<int> defaultVector;
int valuedInt;
vector<int> sizedVector;
public:
Example(int value = 0, size_t vectorLen = 10)
: defaultInt(), defaultVector(), valuedInt(value), sizedVector(vectorLen)
{
//defaultInt is now 0 (since integral types are default-initialized to 0)
//defaultVector is now a std::vector<int>() (a default constructed vector)
//valuedInt is now value since it was initialized to value
//sizedVector is now a vector of 'size' default-intialized ints (so 'size' 0's)
}
};
For kicks and giggles, you could also do thirdVector(vectorLen, value) to get a vector with vectorLen elements with the value value. (So Example(5, 10) would make thirdVector a vector of 10 elements valued 5.)

My question is what is v() and why do people do that since that doesn't look like v is initialized into a value either
This is sometimes done to be more explicit. For non POD types this is not necessary as the default constructor is automatically called for them. If the types default constructor has not been defined or is not accessible, this will cause a compile error.
This makes the most sense for POD types, as their value is undefined when they are not initialized.
struct A
{
int t;
A() : { /* value of t undefined */ }
}
struct A
{
int t;
A() : t() { /* value of t is t's default value of 0 */ }
}

Related

Is it allowed to assign from an object that has some indeterminate values?

Consider the following class, which can either be in a "non-empty" or "empty" state, and in the "empty" state the other member is default initialized (hence has an indeterminate value):
struct MaybeInt {
bool has_value;
int value;
MaybeInt() : has_value(false) {}
MaybeInt(int v) : has_value(true ), value(v) {}
};
Is it allowed to assign from a default-constructed MaybeInt, as in:
MaybeInt empty, another;
another = empty; // OK?
How about construction?
MaybeInt empty, another(empty); // OK?
Does the answer change if MaybeInt::value has type char?
another = empty is indeed UB because empty.value has an indeterminate value and because the implicitly defined copy constructor of a class copies all members.
The trick is to place the member value in a union. This way the implicitly defined copy constructor of the union copies the object representation :
struct MaybeInt {
bool has_value;
union {
int value;
char _dumb;
};
MaybeInt() : has_value(false) {}
MaybeInt(int v) : has_value(true ), value(v) {}
};
NB: This is a low level trick to have an optional that is trivially copyable. This is realy important for code execution speed. This class can be passed through function call on a cpu register, while it would be impossible if it were not trivially copyable.
Your code results in undefined behaviour.
MaybeInt empty, another;
another = empty;
OK. another has the same undefined value that empty had. Technically working, leading to bugs in practise.
MaybeInt empty, another(empty);
empty contains an undefined value, and this is copied to another which has the same value, only now your class thinks it is defined.
Use std::optional,
everything which should be optional is solved in a standard way. (C++ 17)
optional<int> x; // empty or not, you can use `value_or()` to get a default value.

std::vector initialization of class member

I have read that std::vector always initializes it's objects with their default values say for an int it is 0. The same should be applicable even for classes where the default constructor is called. However the results shown by a test program are a bit different :-
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
int i;
A(){};
};
class B
{
public:
int i;
B() = default;
};
template <typename T>
void seev (const vector<T> &v)
{
for (auto &x:v)
{
cout << x.i << ' ';
}
cout << '\n';
}
int main()
{
vector<int> vi(5); // just to show that std::vector always default initializes values & hence int will be 0 here
vector<A> va(5);
vector<B> vb(5);
for (auto x: vi)
cout << x << ' ';
cout << '\n';
seev (va);
seev (vb);
return 0;
}
The output is :-
0 0 0 0 0
8854016 0 8847696 0 8854016
0 0 0 0 0
My question is why was the value of member i undefined for A & not for B ? What difference did the constructor signatures :-
A() {}
&
B() = default;
make ?
There is a difference between default initialization and value initialization. The difference is shown for B but not for A:
When a class has a non-= defaulted constructor, this constructor is called when either default or value initialization is used. Its responsibility is to initialize all members appropriately.
When a class either has no constructor or an = defaulted default constructor the initialization of the members depends on how the object is constructed: when initializing the object without parenthesis default initialization is done which leaves subobjects without a default constructor uninitialized. When initializing the object with parenthesis value initialization is done which value initializes all subobjects. Value initialization of built-in types mean they get a suitable zero value while default initialization of built-in types means they are left uninitialized.
Since A has an explicitly written default constructor, its constructor needs to initialize all members which aren't of class type with a default constructor. B has an implicitly written default constructor and default or value initialization is performed as necessary for the subobjects.
The objects in a std::vector<T> are constructed using T() (if no other arguments are provides as is the case, e.g., for push_back() or emplace_back()). For A members that means they are left uninitialized, for B members that means they are zero initialized.
When creating a vector with a specific size, the vector doesn't set default values, it uses something called value initialization which is something completely different.
For primitive types, like e.g. int, that means the value will be zero-initialized (i.e. zero). But for objects with constructors the default constructor (if it has one) will be used.
In the case of your A class, the default constructor does not initialize the i member variable, so it will be uninitialized and have an indeterminate value, by printing that value you have undefined behavior.
As for the B class it's a POD type which means that value initialization of the whole object will also value-initialize all members. B is a POD type because it has a trivial default constructor (which A does not have).
A() {};
You are not initializing A::i in A() so its value is unspecified after value initialization. This behaviour is different to what you would get with a compiler-provided default constructor.
A() = default;
Defining as default on the other hand has the effect of providing a constructor with the same semantics of the compiler-synthesized default constructor. That is to say, A::i would get value-initialized (and therefore zero-initialized) when an A object is value initialized with expressions such as A() or A{}.
Note: this can be fixed by either dropping the definition of the default constructor, define it as default, or explicitly initialize i.
struct A
{
// Compiler provided A() will initialize i
// when A is value initialized
int i;
};
or, equivalently in terms of initialization semantics,
struct A
{
int i;
A() = default; // useful if other constructors defined
};
An "empty" constructor will not initialize anything, so the fact that va contains zero is just luck/coincidence. The default constructor will do nothing either, so they will produce the same thing. What you are seeing is undefined behaviour (or unspecified behaviour perhaps - as we shouldn't really expect world war three to break out because of an uninitialized variable).
The problem is that std::vector is supposed to call the default constructor & not set the values by default. The default constructor of int sets it's value to 0 hence you get a std::vector<int> with all values initialized with 0. However you default constructor of A is empty & hence it doesn't initialize i with any value. If you define the constructor as :-
A() { i = 0; }
Then your vector would definitely initialize the i of the objects of A with 0. What default does is this :-
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
A()
{
cout << "Default constructed A\n";
}
A (A &obj)
{
cout << "Copy constructed A\n";
}
};
class X
{
int x;
double d;
bool b;
A obj;
public:
X() = default;
void show()
{
cout << fixed << x << ' ' << d << ' ' << b << '\n';
}
};
int main()
{
cout << boolalpha;
vector<X> v(1);
v[0].show();
return 0;
}
/* Output :-
Default constructed A
0 0.000000 false
*/
What B() = default; roughly means as far I get from this test code that it is a replacement for :-
B()
{
i = 0;
d = 0.00;
b = false;
// obj is default initialized
}
ie the initialization of the data members with their default values (for primitives) or default constructors (for classes). But this is only for runtime initialization which means for locally created objects the definition is same as B() = {}.
So the thing was that class A had a user defined constructor (with an empty body) & hence didn't initialize because that's how the programmer defined it. On the other hand class B had a constructor that was left on to the compiler to call implicitly. Hence for local objects it didn't do any specific initialization but for dynamic objects (like that of a std::vector) it did the value type initialization.

Calling non default template constructor in a Class constructor

Is it possible in the following example to call the "not default constructor" of class A for every element of mVector within the constructor of class B ?
class A {
public:
A (int n) {/*stuff*/}
};
class B {
public:
B (): mVector(10) {} //call A(int n) constructor?
private:
vector<A> mVector;
};
If you want to set all the elements to the same value, there's a constructor for that
mVector(10, 42) // 10 elements initialised with value 42
If you want to set the elements to different values, use list initialisation
mVector{1,2,3,4,5,6,7,8,9,10} // 10 elements with different values
Strictly speaking, this doesn't do exactly what you describe; it creates a temporary T, and then uses that to copy-initialise each vector element. The effect should be the same, unless your type has weird copy semantics.
You could do one thing here:-
B() : mVector(10, A(10))
{
}
Or
B() : mVector(10, 10)
{
}
Both are essentially the same thing. However, former one is more efficient.
You can use the constructor overload of std::vector taking an element count and a value which for your use case is equivalent to:
std::vector(size_type count, const T& value);
Use it to initialize the elements with the value type's non default constructor:
std::vector<A> mVector(10, A{0}); // 10 elements copy initialized using 'A{0}'.
Or when initializing in the initialization list:
B() : mVector(10, A{0}) {}

Initialisation and assignment

What EXACTLY is the difference between INITIALIZATION and ASSIGNMENT ?
PS : If possible please give examples in C and C++ , specifically .
Actually , I was confused by these statements ...
C++ provides another way of initializing member variables that allows us to initialize member variables when they are created rather than afterwards. This is done through use of an initialization list.
Using an initialization list is very similar to doing implicit assignments.
Oh my. Initialization and assignment. Well, that's confusion for sure!
To initialize is to make ready for use. And when we're talking about a variable, that means giving the variable a first, useful value. And one way to do that is by using an assignment.
So it's pretty subtle: assignment is one way to do initialization.
Assignment works well for initializing e.g. an int, but it doesn't work well for initializing e.g. a std::string. Why? Because the std::string object contains at least one pointer to dynamically allocated memory, and
if the object has not yet been initialized, that pointer needs to be set to point at a properly allocated buffer (block of memory to hold the string contents), but
if the object has already been initialized, then an assignment may have to deallocate the old buffer and allocate a new one.
So the std::string object's assignment operator evidently has to behave in two different ways, depending on whether the object has already been initialized or not!
Of course it doesn't behave in two different ways. Instead, for a std::string object the initialization is taken care of by a constructor. You can say that a constructor's job is to take the area of memory that will represent the object, and change the arbitrary bits there to something suitable for the object type, something that represents a valid object state.
That initialization from raw memory should ideally be done once for each object, before any other operations on the object.
And the C++ rules effectively guarantee that. At least as long as you don't use very low level facilities. One might call that the C++ construction guarantee.
So, this means that when you do
std::string s( "one" );
then you're doing simple construction from raw memory, but when you do
std::string s;
s = "two";
then you're first constructing s (with an object state representing an empty string), and then assigning to this already initialized s.
And that, finally, allows me to answer your question. From the point of view of language independent programming the first useful value is presumably the one that's assigned, and so in this view one thinks of the assignment as initialization. Yet, at the C++ technical level initialization has already been done, by a call of std::string's default constructor, so at this level one thinks of the declaration as initialization, and the assignment as just a later change of value.
So, especially the term "initialization" depends on the context!
Simply apply some common sense to sort out what Someone Else probably means.
Cheers & hth.,
In the simplest of terms:
int a = 0; // initialization of a to 0
a = 1; // assignment of a to 1
For built in types its relatively straight forward. For user defined types it can get more complex. Have a look at this article.
For instance:
class A
{
public:
A() : val_(0) // initializer list, initializes val_
{}
A(const int v) : val_(v) // initializes val_
{}
A(const A& rhs) : val_(rhs.val_) // still initialization of val_
{}
private:
int val_;
};
// all initialization:
A a;
A a2(4);
A a3(a2);
a = a3; // assignment
Initialization is creating an instance(of type) with certain value.
int i = 0;
Assignment is to give value to an already created instance(of type).
int i;
i = 0
To Answer your edited Question:
What is the difference between Initializing And Assignment inside constructor? &
What is the advantage?
There is a difference between Initializing a member using initializer list and assigning it an value inside the constructor body.
When you initialize fields via initializer list the constructors will be called once.
If you use the assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.
As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.
For an integer data type or POD class members there is no practical overhead.
An Code Example:
class Myclass
{
public:
Myclass (unsigned int param) : param_ (param)
{
}
unsigned int param () const
{
return param_;
}
private:
unsigned int param_;
};
In the above example:
Myclass (unsigned int param) : param_ (param)
This construct is called a Member Initializer List in C++.
It initializes a member param_ to a value param.
When do you HAVE TO use member Initializer list?
You will have(rather forced) to use a Member Initializer list if:
Your class has a reference member
Your class has a const member or
Your class doesn't have a default constructor
Initialisation: giving an object an initial value:
int a(0);
int b = 2;
int c = a;
int d(c);
std::vector<int> e;
Assignment: assigning a new value to an object:
a = b;
b = 5;
c = a;
d = 2;
In C the general syntax for initialization is with {}:
struct toto { unsigned a; double c[2] };
struct toto T = { 3, { 4.5, 3.1 } };
struct toto S = { .c = { [1] = 7.0 }, .a = 32 };
The one for S is called "designated initializers" and is only available from C99 onward.
Fields that are omitted are automatically initialized with the
correct 0 for the corresponding type.
this syntax applies even to basic data types like double r = { 1.0
};
There is a catchall initializer that sets all fields to 0, namely { 0 }.
if the variable is of static linkage all expressions of the
initializer must be constant expressions
This {} syntax can not be used directly for assignment, but in C99 you can use compound literals instead like
S = (struct toto){ .c = { [1] = 5.0 } };
So by first creating a temporary object on the RHS and assigning this to your object.
One thing that nobody has yet mentioned is the difference between initialisation and assignment of class fields in the constructor.
Let us consider the class:
class Thing
{
int num;
char c;
public:
Thing();
};
Thing::Thing()
: num(5)
{
c = 'a';
}
What we have here is a constructor that initialises Thing::num to the value of 5, and assigns 'a' to Thing::c. In this case the difference is minor, but as was said before if you were to substitute int and char in this example for some arbitrary classes, we would be talking about the difference between calling a parameterised constructor versus a default constructor followed by operator= function.

Variable initialising and constructors

I am currently taking a c++ course and trying to get a deep understanding of the whole thing.
I came up with some theories, it would be great if somebody could confirm them:
Every variable (local,global,staic,member and non-member) is guaranteed to have its ctor called before first use
The ctors of primitives like int are essentially no-ops, so we have explicitly assign a value, there is no default zero value.
the following classes are semantically the same (and should generate identical code)
class A
{
int n;
};
and
class A
{
int n;
public:
A() : n() {}
};
and
class A
{
int n;
public:
A() { n = int(); }
};
The variable n is in every case still uninitialized.
EDIT:
It seem that I absolutetly underestimated the complexity of this subject, most of my assumptions were wrong. Now Iam still trying to find out the basic rules of object initialisation.
I'm afraid you are wrong. When you say:
int n = int();
Then n (and all other POD types) will zero initialised.
Also, make sure that you are very clear in your mind about the difference between initialisation and assignment - this is quite important in C++:
int n = int(); // initialisation
n = 0; // assignment
You might find this interesting.
The difference between new Foo and new
Foo() is that former will be
uninitialized and the latter will be
default initialized (to zero) when Foo
is a POD type. So, when not using the
form with the parens, the member "a"
can contain garbage, but with the
parens "a" will always be initialized
to 0.
No, the variable is only left uninitialised in the first case.
For a member that is a class with a user-defined constructor, the situation is simple: a constructor is always called.
Built-in types (and 'plain old data' structs) may be left uninitialised, as in your first example. Although they don't have user-supplied constructors, using the construction syntax (your other two examples) initialises them to zero.
The reason for this slightly tricky rule is to avoid undue overhead; for example if you defined:
struct S
{
int array[1024*1024];
};
with the intention of only assigning values as you needed them, you wouldn't want the compiler to whitewash 4Mb of memory with zeroes whenever you construct one.
class A
{
int n;
};
Only memory is allocated, no initialization done for n.
class A
{
int n;
public:
A() : n() {}
};
Here n is initialized with 0.
class A
{
int n;
public:
A() { n = int(); }
};
Here n is first constructed (without any default value),
then int() causes a temp int to be created with value 0
Which is then assigned to n;