1


I read following links :-

object initialized with and without parentheses

types of default constructor

diff b/w value,zero and default intilization

I have some question which i want to clarify.


1) Given a POD class , say :-

class A{
        int val;
};


If i create an object of type A.

A obj; // will this call implicitly defined constructor provided by compiler ?
Now as far as my understanding in this case constructor is not called.is it correct?

new A(); // value-initialize A, which is zero-initialization since it's a POD. Now in this case will implicitly defined constructor provided by compiler ? Is there is any role of constructor for zero initializing the object?

If my understanding is wrong , could you please give me an example where implicitly defined defined constructor is not called at all.

Thank you in advance.

Jonas
  • 121,568
  • 97
  • 310
  • 388
user1057741
  • 265
  • 1
  • 2
  • 10

2 Answers2

3

1) Correct. obj.val is not initialized.

2) This is a function declaration, not an initialization:

A obj(); // function obj() returning an A

If you did this,

A obj{};     //C++11
A obj = A(); // C++03 and C++11

obj would be value-initialized, and so would obj.val. This in turn means that obj.val would be zero-initialized (value-initialization means zero-initialization for built-in types).

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • so for A obj; will not call implicitly defined default constructor? – user1057741 Nov 11 '13 at 17:27
  • @user1057741 it will, but that won't result in any initialization of `val`. If your class had a member of a user defined type (say, an `std::string`), then its default constructor *would* be called. – juanchopanza Nov 11 '13 at 17:29
  • could you please give me an example where implicitly defined default constructor is not called ? – user1057741 Nov 11 '13 at 17:31
  • @user1057741 No, I can't think of one at the moment. – juanchopanza Nov 11 '13 at 17:45
  • does value-initialization results into zero-initialization every time ? – user1057741 Nov 11 '13 at 17:56
  • @user1057741 there are pathological cases where it doesn't. For example, if you have a class with a built-in data member, and you provide a default constructor that does not explicitly initialize it, value initializing that class does not zero-initialize the member. I put a contrived example [here](http://ideone.com/TqNYue). When I ran an optimized build of that on my platform, I get `-1924868947`. If you removed the default constructor, or used it to value-initialize `i`, then `i` would be zero initialized. – juanchopanza Nov 11 '13 at 20:27
  • @ juanchopanza : thank you for the example, so if i am understanding it correctly , it will be value-initialized but because user have provided default constructor which does not do anything hence it will be remain uninitialized. – user1057741 Nov 12 '13 at 04:35
2
A obj;

It calls default constructor (or even not for optimization), however default constructor doesn't initialize it.

 

A obj();

It's a function declaration. No arguments and returns A.

 

A obj{};

Instead, you can use above code which sets val to zero.

masoud
  • 55,379
  • 16
  • 141
  • 208