2

In cpp, we can have primitive data-types initialized as

int a(32);

How does this constructor initialisation work? Does C++ treat it as an object?

2
  • What exact do you mean by "treats it as an object"? When it gets compiled it will move the literal constant 32 to the variables memory slot. Nothing more. So its just a syntax thing. Commented Jun 1, 2012 at 2:40
  • 1
    No, in C++ the primitives are not treated as objects. As far as integers are concerned, this is just an alternative syntax for a=32. Commented Jun 1, 2012 at 2:41

2 Answers 2

3

This is best described in:

C++03 8.5 Initializers
Para 12 & 13:

.......
The initialization that occurs in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called
direct-initialization and is equivalent to the form

T x(a);

If T is a scalar type, then a declaration of the form

T x = { a };

is equivalent to

T x = a;

In the question the type is int which is a scalar type.

Sign up to request clarification or add additional context in comments.

Comments

2

This is so-called direct-initialization. In C++, integers are not objects and the expression that you write here is not a constructor. It just initializes a to the value of 32.

3 Comments

Got confused by the statement "The other way to initialize variables, known as constructor initialization, is done by enclosing the initial value between parentheses (()):" given here at cplusplus.com/doc/tutorial/variables
@vagrawal13: Yet another reason to avoid cplusplus.com.
@vagrawal13: that statement is somewhat confusing indeed. For scalar types, the difference between the two different kinds of initialization is syntactic only. For objects of class type, the situation is more subtle. See Is there a difference in C++ between copy initialization and assignment initialization? for a good read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.