Hi
I would like to know which is better. inialise a value like
int i=0; or like this.
int i(0); and why.
Short answer for : for built-in scalar types there is no semantic difference between int i = 0; and int i(0);. For class types the two forms can behave differently because T x = y; is copy-initialization while T x(y); is direct-initialization; copy-initialization does not consider explicit constructors. was correct to call out explicit — that is the primary visible difference you’ll hit for user types. C++ initialization rules. (cppreference.net)
A clarification to : initialization constructs objects (constructors and, in copy-initialization, copy/move constructors); it does not call the assignment operator for a brand-new object. The assignment operator is used only when assigning into an already-initialized object. See how copy-initialization and constructors are selected. (cppreference.com)
Watch out for the “most vexing parse”: some parenthesized declarations are parsed as function declarations, not object creation. For example:
Widget w(); // declares a function, not an object
Widget w{}; // constructs an object (preferred in modern C++) Using braces {} (uniform initialization) avoids that ambiguity. (w.cppreference.com)
Modern practical advice: prefer brace (list) initialization in C++11 and later for a consistent rule set and to prevent narrowing conversions, but be aware that brace-initialization may prefer std::initializer_list-taking constructors in overload resolution (so overload behavior can change). For details on list-initialization and narrowing rules, see list-initialization. ’s GOTW pointer is a good historical supplement to this modern view. (docs.cppreference.com)
Jump to Post— Dave Sinkula 2,398For native types I don't think it makes a difference. For user-defined types I think it may mean the difference (if any) between calling (the default constructor and [probably not in this case?]) the assignment operator and a copy constructor.
Real C++ people...?
Jump to Post— Narue 5,707>I would like to know which is better.
There's no difference if they both compile (they might not for user-defined types), pick which you find more intuitive.>Real C++ people...?
I don't think I count as a real C++ person, but both call the primary (non-copy) constructor. The only …
For native types I don't think it makes a difference. For user-defined types I think it may mean the difference (if any) between calling (the default constructor and [probably not in this case?]) the assignment operator and a copy constructor.
Real C++ people...?
>I would like to know which is better.
There's no difference if they both compile (they might not for user-defined types), pick which you find more intuitive.
>Real C++ people...?
I don't think I count as a real C++ person, but both call the primary (non-copy) constructor. The only noticeable difference would be if the constructor in question is defined as explicit, in which case an implicit argument would be illegal:
class test1 {
public:
test1 ( int init ) {}
};
class test2 {
public:
explicit test2 ( int init ) {}
};
int main()
{
test1 a1 ( 0 );
test1 b1 = 0;
test2 a2 ( 0 );
test2 b2 = 0; // This will cause an error
} thank you everyone for the answer
Thanks, I needed that.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.