Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Simple operator overloading question  (Read 2581 times)

0 Members and 1 Guest are viewing this topic.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Simple operator overloading question
« on: July 29, 2011, 06:18:00 am »
Hello, I was reading the following page from the wiki:
http://www.sfml-dev.org/wiki/en/sources/richtext

On the following function:
Code: [Select]
RichText & RichText::operator << (const sf::Color &color)
{
myCurrentColor = color;
return *this;
}

What is the "return *this" doing here exactly? Isn't the first line "myCurrentColor = color;" enough to set a new color value?

I know this is probably a simple matter but I came from a C programming background and I haven't mastered Operator Overloading too well yet.

Thanks in advance for the help,
Regards,
Alex.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Simple operator overloading question
« Reply #1 on: July 29, 2011, 06:48:29 am »
I'm pretty sure it does the following:

It returns the current object, so that you can chain "<<" operators.
example:
myText << "Yo" << green << "now im feeling blue" << blue;

If you didn't return the object, you would have to do each << in one statement, instead of chaining them like this.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Simple operator overloading question
« Reply #2 on: July 29, 2011, 07:51:26 am »
I think that's exactly it.
Thank you!

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Simple operator overloading question
« Reply #3 on: August 24, 2011, 09:38:54 pm »
Hey, Sorry for bumping the topic again but I've ran into another problem.
I'm was trying to make a class that worked like std::cout but for displaying text easily with SFML.

I did the following for outputting:
Code: [Select]
template<class T>Label & Label::operator << (T &input)
{
std::ostringstream ss;
ss << input;
   //text is an sf::Text Object from the class
text.SetString(ss.str());
return *this;
}


This works if I do, for example:
Code: [Select]
double i=0.0;
LabelObject << i;


But it doesn't work if I do:
Code: [Select]
LabelObject << 0.0;
I get the error "no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)".
I'm not sure what's the difference and the problem here. What could be wrong?

Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Simple operator overloading question
« Reply #4 on: August 24, 2011, 10:46:39 pm »
Your operator takes a non-const reference, therefore it only accepts named variables. And "0.0" is a temporary, not a named variable. Non-const reference should only be used if you want to modify the variable inside the function.

You should take the argument either by value or by const reference.

And I think you'd better have a generic toString() function, instead of such an operator overload. Your implementation is misleading: it replaces the contents of the text instead of concatenating to the current string.
Laurent Gomila - SFML developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Simple operator overloading question
« Reply #5 on: August 26, 2011, 01:15:57 am »
Oh boy, this isn't the first time I forget to use const references.

Quote
Your implementation is misleading: it replaces the contents of the text instead of concatenating to the current string.

I know, In this case I wanted it to overwrite the previous value but you're right, the operator can be misleading. I think I'll end up changing it to a function.

Thank you.