Questions tagged [default-initialization]

This is the initialization performed when a variable is constructed with no initializer.

11 questions
7
votes
1 answer

Ambiguity between default-initialization and value-initialization

I found many articles explaining the difference between "default-initialization and value-initialization" but in fact I didn't understand clearly. Here's an example: class A{ public: int x; }; int main(){ A a;// default initialization…
6
votes
6 answers

How to let a variable be dependent on other variables inside a class?

What is wrong with the variable international_standard_book_number? How can I make it that it changes, whenever isbn_field_i changes? #include #include class ISBN { private: unsigned int isbn_field_1 = 0; unsigned int…
SAJW
  • 235
  • 2
  • 10
5
votes
1 answer

Is the Order of Array Element Default-Initialization Defined?

When an array is default initialized, is the order in which it's elements are default initialized defined by the C++ standard? To give an example, is the following C++ program guaranteed do print strictly ascending memory addresses? #include…
4
votes
2 answers

Safely initializing a std::array of bools

Given this array declaration and initialization: std::array invalid_params{}; Can I assume that all the elements in the array will always be initialized to false, or is it better to do it explicitly?
anastaciu
  • 23,467
  • 7
  • 28
  • 53
3
votes
0 answers

default initialization, value initialization and in-class-initialization of object not getting properly?

You might feel code is long and I write lot of things but believe me it is quite simple. I referred these two answers on stack-overflow for understanding this. post1 & post2 Code #include class A { public: int…
3
votes
2 answers

Why do designated initializers zero-initialize the data members?

Below is from cppref of Designated initializers: struct A { int x; int y; int z; }; A b{.x = 1, .z = 2}; // ok, b.y initialized to 0 By default, all fundamental types are default-initialized rather than zero-initialized in C++. Why do designated…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
2
votes
0 answers

Lifetime extension of default initialized const ref& in an aggregate

I know that binding prvalues to const ref variables will extend the lifetime of the temporary. Now I thought I'd be clever to have a struct options that contains a const ref field B_options (which is used for initializing a subclass in my context).…
2
votes
1 answer

Why member of class type needs initialization?

I am reading about constructors in C++. I came across this example: #include using namespace std; class NoDefault { public: NoDefault(const std::string&); }; struct A { NoDefault my_mem; }; int main() { A a; …
0
votes
1 answer

Initializing an array of objects created on the heap

Given the non trivial data structure: claas MyClass { public: MyClass():x(0), p(nullptr) {} private: int x; int* p; }; Is there any guarantee provided by the c++ specification that the default constructor will be called for each instance…
Sampath
  • 1,144
  • 1
  • 21
  • 38
0
votes
1 answer

c++11 - zero-initi of members insted of default-init

In the below code, I expect members of a being inited with gargabe as they are not mentioned in the members-init-list of the called constructor (with two int parameters). Instead, I'm constantly getting 0 in both i and j of a, b and c and I am…
0
votes
1 answer

question about default initialization in c

I am studying for an interview and found this question a bit bewildering. Would appreciate your advice. What is not initialized by default? The last variable of a static array in which the first variables are explicitly initialized in a…
jrz
  • 1,213
  • 4
  • 20
  • 54