What is the difference between these two types of assingment?

(a)

string color("red");

(b)

string color="red";

Recommended Answers

All 8 Replies

Nothing, you are constructing not assigning.

One is using the constructor to initialize the value of the string object and the other is using the overloaded operator "=" to assign that value to the string object.

I'm sorry to say that both answers are wrong.

Example (a) uses the overloaded constructor that matches the argument, which in this case has type const char*. The string type is actually an instantiation of the basic_string template, so that string is equivalent to basic_string<char>; but putting that detail aside, it is useful to think of the first example as asking for string(const char*).

Example (b) uses = along with an initializer, which means that it is a request to use the copy constructor (conceptually string(const string&)). The expression after the initializer is converted to type string, which means that the second example is equivalent to

string color = string("red");

Theoretically, then, it is a request to construct a temporary object of type string, initialize color as a copy of that object, and then destroy the temporary object.

However, the compiler is permitted to optimize away this copy (provided that the relevant copy constructor is accessible, which in this case it is), in which case the two examples may well generate identical code.

So the first answer, saying that there is no difference between these two examples, is not quite right; and the second, saying that it is using overloaded operator =, is just plain wrong.


However, the compiler is permitted to optimize away this copy (provided that the relevant copy constructor is accessible, which in this case it is), in which case the two examples may well generate identical code.

Thanks for explaining that..

Thanks arkoenig. Practically, when using them, it doesn't matter which to use?

In the case you've shown, it doesn't matter. However. if you want to use a constructor that takes more than one argument, you have to use the first form. For example:

string stars(10, '*');

defines a variable and initializes it to ten '*' characters. If you were to use form (b), you would have to write

string stars = string(10, '*');

because if you were to write

string stars = 10, '*';

it would be a syntax error, and if you were to write

string stars = (10, '*')

it would be equivalent to writing

string stars = '*';

which is a type error (because there is no string constructor that takes a single char as its argument).

I see. So if the string is not a sequence of the same character (such as 10 star symbols), but rather something like a word, then no matter which? Even in the cases where it makes no difference, is there any effect on storage? Thanks.

In the particular original examples, the eventual result is the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.