difference between int a=123 and int a=(123)

Hi,

Please explain me what is the difference between
int a=123;
int a=(123);

Please explain all possible differeances like memory management or anyother.
Last edited on
Perhaps you were thinking about:

int a(123); // no = sign

There's no difference what method you use to initialize, the style with the = is the C style, and the one with the () is the C++ constructor style.

Another example:

1
2
3
4
5
6
#include <string>

// ...

std::string s1 = "Hello, "; // C style
std::string s2("World!"); // C++ style 


In both cases, the same thing happens, the std::string constructor is called.
string (const char* s);

http://www.cplusplus.com/reference/string/string/string/

For built-in types, such as int and char, they're simply initialized with the value.

In your example, the parentheses don't change anything, and you can put how many you want:

1
2
3
int a = ((((123)))); // same as
int a = (123); // same as
int a = 123;

Hi,

Thanks for your explanation but some where I read(red) it there is some difference while storing in memory.

I searched in google for that article but I did not get, if possible pls explain me.
Maybe what you read was wrong.

Let's review. @Catfish666 gave you a clear and succinct response that differs from your fuzzy memories of an article you read some time ago on the internet (but you can no longer find it). You want us to either find that article or guess what it said and then justify its contents against what @Catfish666 provided you.

Good luck with that.
Last edited on
> I read(red) it there is some difference

What you had read about was probably the technical difference between direct initialization and copy initialization.

1
2
int a(123) ; // direct initialization
int a = 123 ; // copy initialization 

There is no difference between the two in this case - an int being initialized with an expression of type int.

There is a difference if class types are involved:
1
2
3
4
5
6
7
8
// direct imitialization 
// http://en.cppreference.com/w/cpp/language/direct_initialization
std::unique_ptr<int> p1( new int(100) ) ; // fine

// copy initialization - less permissive than direct imitialization
// http://en.cppreference.com/w/cpp/language/copy_initialization
std::unique_ptr<int> p2 = new int(100) ; // *** error
// ** error: no implicit conversion from 'int*' to 'std::unique_ptr<int>' 
@ JLBorges: doesn't that happen just because std::unique_ptr constructor from pointer is marked explicit?

explicit unique_ptr( pointer p );

http://en.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr
> doesn't that happen just because std::unique_ptr constructor from pointer is marked explicit?

Yes. That is the difference between direct initialization and copy initialization.
To quote:
Implicit conversion is defined in terms of copy-initialization: if an object of type T can be copy-initialized with expression E, then E is implicitly convertible to T.
Hi,

Thanks to all for giving very very good explanation.
Topic archived. No new replies allowed.