开发者

Explicit Assignment vs Implicit Assignment

I'm reading a tutorial for C++ but it didn't actually give me a difference (besides syntax) between the two. Here is a quote from the tutorial.

You can also assign values to your variables upon declaration. When we assign values to a variable using the assignment operator (equals sign), it’s called an explicit assignment:

int nValue = 5; // explicit assignment

You can also assign values to variables using an implicit assignment:

int nValue(5); // implicit assignment

Even though implicit assignments look a lot like function calls, the compiler keeps track of which names are variables and which are functions so that they can be resolved properly.

Is there a differenc开发者_JAVA百科e? Is one more preferred over the other?


The first is preferred with primitive types like int; the second with types that have a constructor, because it makes the constructor call explicit.

E.g., if you've defined a class Foo that can be constructed from a single int, then

Foo x(5);

is preferred over

Foo x = 5;

(You need the former syntax anyway when more than one argument is passed, unless you use Foo x = Foo(5, "hello"); which is plain ugly and looks like operator= is being called.)


For primitive types both are equivalent, it is for user defined class types that there is a difference. In both cases the code that gets executed will be the same (after basic optimizations are performed), but the requirements on the types differ if the element from which we are initializing is not of the type that we are constructing.

The copy-initialization (T t = u;) is equivalent to copy construction from a temporary of type T that has been implicitly converted from u to t. On the other hand direct-initialization is equivalent to a direct call to the appropriate constructor.

While in most circumstances there will be no difference, if the constructor that takes the u is declared explicit or if the copy-constructor is inaccessible, then copy-initialization will fail:

struct A {
   explicit A( int ) {}
};
struct B {
   B( int ) {}
private:
   B( B const & );
};
int main() {
   A a(1);      // ok
   B b(1);      // ok
// A a2 = 1;    // error: cannot convert from int to A
// B b2 = 1;    // error: B( B const & ) is not accessible
}

For some historical background, initially primitive types had to be initialized with copy-initialization. When *initializer-list*s were added to the language to initialize member attributes from a class, it was decided that primitive types should be initialized with the same syntax that classes to keep the syntax in the initializer list uniform and simple. At the same time allowing the initialization of classes by means of copy-initialization makes user defined types closer to primitive types. The differences in the two initialization formats comes naturally: int a = 5.0; is processed as a conversion from 5.0 to int, and then initialization of a from the int. The same goes with user defined types: T u = v; is processed as conversion from v to T, and then copy construction of u from that converted value.


when you are declaring a variable and initializing it, they are functionally the same in that context. I usually refer to the two as:

int nValue = 5; // assignment syntax

and

int nValue(5); // construction syntax

For basic types, I prefer assignment over construction since it is more natural, especially for those who have programmed in other languages.

For class types, I prefer construction syntax since it obviates the existence of a constructor function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜