If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Studying operator overloading

I'm studying operator overloading and can not understand how that is identified using the + + operator pre-set or post-set.

First, use proper code tags when posting code. There is no need for "Indent" tags if you just posted using code tags:

[code]
Your code goes here
[/code]

Example:

Code:

#include <iostream>
int main()
{
std::cout << "Hello World";
}

See how the code looks when I post as compared to when you post code?

Second, if you're studying operator overloading, why are you using such a confusing and wrong piece of code to study this? Please see the FAQ on overloading ++ here, and compare the simple example and explanation of this operator and how it is to be overloaded to what you're doing.

Why are you passing string2 objects by value? You should be passing them by const reference, not by value.

Code:

string2 operator +=(string2 s) const //Concatena

Why is operator += returning a new object? It should be returning a reference to the existing object. That is the whole purpose of +=. It takes the existing object, adds something to it, and returns the existing object.

Everything I pointed out is covered in good books. Are you learning from real books? If you're just throwing things together without any guidance, that is not the way you learn C++. You're supposed to be using good books (and tutorials) on the language, i.e. see how the experts in the language do things. Putting stuff together yourself just leads to mistakes and a misunderstanding of how to use C++ properly.