Explicit Assignment vs Implicit Assignment

南楼画角 提交于 2019-12-20 10:36:44

问题


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 difference? Is one more preferred over the other?


回答1:


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.)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6751850/explicit-assignment-vs-implicit-assignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!