1

I'm studying the concept of objects and classes in c++, while doing so, I noticed something about the concatenation.

I just want to clarify what's happening behind this and what's the difference between the two.

I tried doing both and it seemed that the '+' doesn't work properly on the age part which is an integer but works on the strings.

Code:

 man.name = "john wick";
 man.age = 32;

 cout << "The man's name is: " << man.name << endl;
 cout <<  "The man's age is: " << man.age << endl;

Output that I get using '<<':

The man's name is: john wick
The man's age is: 32

and If I change the '<<' into '+' in the man.age, this is what I'm getting.

Output that I get using '+':

The man's name is: john wick
@@
Albert
  • 19
  • 4
  • 5
    There is no concatenation operator for strings and integers in C++. What you have inadvertently stumbled upon is *pointer arithmetic* on a string literal, which would take far too long to explain here. Consult your favourite C++ book. – john Aug 01 '19 at 08:39
  • @formerlyknownas_463035818 thank you! I just edited the post and the reason why im having "is standing" is I accidentally include the next lines of codes in the question that is not relevant. – Albert Aug 01 '19 at 08:45
  • @john alright, thank you! – Albert Aug 01 '19 at 08:47
  • That your code displayed `n is standing.` is the same reason why it now displays `@@`. By adding `32` to `The man's age is: ` ( that is a `const char * ` ) you move that pointer by `32`, pointing to a different address in memory. In your original question to another const string that you used somewhere else ending with `n is standing.` in your current version of the question, it is a place in memory where `@@` followed by a `\0` exists. – t.niese Aug 01 '19 at 08:53

1 Answers1

1

Your problem is that you do not distinguish between '+' and '<<' operators.

The '+' operator is overloaded for operation with 2 strings, or a string and a const char*, but not for a string and an integer. For example : cout<<"The man's nickname is "+"Jhon"; does not work as expected, because the two operands are const char*.
cout<<"The man's nickname is "+man.nickname; works as expected, because the first operand is a string, and the second is a const char*.
But cout<<"The man's age is "+man.age; can't work properly cause man.age is not a string or a const char*.

The '<<' operator, because it is a stream operator only, may work with more different operands. To understand how it works, let decompose how it works in your small example.
Your line is cout<<"The man's age is "<<man.age;.
What this line do is :

  • cout<<"The man's age is " adds "The man's age is " to the stream cout, by converting this const char*properly.
  • When it's done, cout<<man.age; converts the number and adds it to the stream.

Note that you could overload the '+' operator to add the possibility to concatenate a string and an integer, but I do not recommend this because stream operator is efficient yet for your case.

TUI lover
  • 542
  • 4
  • 16
  • 3
    If you consider `"abc"` a string (which is a c-string, to be precise, not `std::string`), then you cannot overload a `+` operator for a string and an `int`. At least one operand must be a user-provided class, which neither `int` nor `""` are. – Fureeish Aug 01 '19 at 09:06
  • You're right, I corrected it. – TUI lover Aug 01 '19 at 09:21
  • 1
    Then again "*[...] a value noted `"A sample of text"` may be evaluated as `const char *` or a `std::sting` by the compiler depending on the context*" is incorrect. It's always a `const char[]` which decays into `const char*`. It's never `std::string`. `std::string` can be constructed from such argument, but it is not `std::string` itself... – Fureeish Aug 01 '19 at 09:27
  • @Fureeish corrected – TUI lover Aug 01 '19 at 09:30
  • And we're back to the incorrect last paragraph. What's more, you added a statement saying that `operator <<` is very efficient. It's not. It's actually very slow. – Fureeish Aug 01 '19 at 09:36
  • operator << is more efficient than + in this case (display two texts). Are you thinking about another solution or another context? – TUI lover Aug 01 '19 at 09:44
  • 1
    I would be careful about claiming that something is more efficient, especially regarding formatted output. No, I am not thinking of any other solution since I do not attempt to answer this question. I am just pointing out incorrect parts of your answer. – Fureeish Aug 01 '19 at 10:00
  • 1
    I see. So there are conflict between data types.Thank you! – Albert Aug 01 '19 at 10:28