1

I'm having trouble understanding some particular behaviour of assignment in strings.

//method 1
std::string s;
s+='a' //This works perfectly

but

//method2
std::string s;
s="" + 'a';//This gives unexpected value

Why 2nd method gives unexpected value ? From what I've read string default constructor initialise string variable as empty string, if no constructor is specified. And s+='a' should be same as s=s+a. So why isn't the method 2 same as method 1?

And one more query on the same topic , if we can't initialise a string with char literal then how can we assign a char literal to it?

std::string s2='a'//gives error while compiling

whereas

std::string s2;
s2='a'//works perfect

From what I understand is we cannot initialise a string variable by char variable because string constructor needs argument of the type(const char *). Why is there not any such restriction while assigning?

5
  • 2
    "foo" is not a std::string, it is a string literal, which is an array of char, more-or-less a char const*. If you want to do std::string type operations on it, you have to turn it into one first: std::string("foo"). Commented Mar 7, 2017 at 8:13
  • BoBTFish is right, try std::string("") + 'a' Commented Mar 7, 2017 at 8:14
  • 1
    Increase the warning Level. I bet you get warnings. Commented Mar 7, 2017 at 8:15
  • Or, if you know about user-defined literals, using namespace std::string_literals; "foo"s + .... Commented Mar 7, 2017 at 8:15
  • @BoBTFish I think now i understand my mistake , thanks for the information. Commented Mar 7, 2017 at 8:19

3 Answers 3

2

For your first query ,

method 1 works perfectly cause in this method you are adding string object type and char literal . and s+='a' , is indeed same as s=s+'a'

focus on the fact that s is string object type rather than string literal.

In the 2nd method , you are adding string literal and char literal . Focus on the difference between the two , In first method there is string object you can add string or char literals to string object type,its one of the features provided by string object type . But you cant add simply add the literals with each other.In c++ , however "StringLiteral1" "StringLiteral2" , will result in the concatenation of the two string literals.

for 2nd query,

Initialisation is not the same as assignment , string object default constructor takes const char * to initialise . Assignment is a completely differenet story(if not,someone please do correct me ).

Sign up to request clarification or add additional context in comments.

2 Comments

"default constructor takes const char *"
Well, it isn't a default constructor if it takes an argument. It's just one of the non-default constructors.
2

"" is a string literal of type const char[], and you are adding the string literal, i.e. the pointer to the first element, '\0', to another character. This will naturally give you something else then you expected.

If you want it to be the same as s += 'a', you'll need to use a std::string literal: s += ""s + 'a';. This works, as ""s is an empty std::string, and you just add another character to it.

Comments

0

When you write s="" + 'a'; Remember that "" is not a std::string but a const char*. And const char* doesn't have a predefined concatenation operator. That's why you are having an unexpected behavior instead of concatenation.

3 Comments

"And const char* doesn't have a predefined + operator." It has. Ever heard about pointer aritmethics?
The OP said that "" + `` a" dose not give an error but unexpected results.
It's still not an error", but unexpected behaviour. If you write "abcd" + 2, you return a pointer to the "c" in the string. If you add 5, you obtain a pointer just after the trailing '\0'. If you add anything greater than 5, you obtain an invalid pointer (I can't remember if the addition is undefined behaviour, or if just *using the pointer is UB). 'a' almost certainly has a value of 97, and adding that to a pointer to a single null character probably ends up pointing at random memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.