C++ << operator use within class

Started by
4 comments, last by bubu LV 12 years, 6 months ago
Hey guys, please help me accomplish the following: I have a class called GameSimulation and I have the << operator set up so i can do
GameSimulation gameObject;
gameObject << "some text";


and the game will output that text in a specific way. Well, that works fine if i do that in main. However, how do i do the same thing from within the class?
For example, here's a function in the class definition:
void GameSimulation::setUpNewPlayer(Player *player)
{ gameWorld[player->location[0]] [player->location[1]] = player->sprite;
???? << "New player has been set up. There are now " << playerList.size() << " players online\n";
}

What do I put in place of the question marks to make it use the GameSimulation::operator << (std::string textString) method that's in the same class? Thanks for your help.

Edit: I tried 'this' and '*this' and neither worked.
Advertisement
Try making the insertion operator a friend function.<br><br>&nbsp;<br>class GameSimulation
{
public:

friend GameSimulation&amp; operator &lt;&lt; (GameSimulation&amp; lhs, const char* s);

void Test()
{
*this &lt;&lt; "Test Text";
}
};

inline GameSimulation&amp; operator &lt;&lt; (GameSimulation&amp; lhs, const char* s)
{
// do something

return lhs;
}<br>
<br><br>Edit: the forum is not formatting or something.<br>

Try making the insertion operator a friend function.<br><br>&nbsp;<br>class GameSimulation
{
public:

friend GameSimulation&amp; operator &lt;&lt; (GameSimulation&amp; lhs, const char* s);

void Test()
{
*this &lt;&lt; "Test Text";
}
};

inline GameSimulation&amp; operator &lt;&lt; (GameSimulation&amp; lhs, const char* s)
{
// do something

return lhs;
}<br>
<br><br>Edit: the forum is not formatting or something.<br>


That has indeed worked, thank you. I've got a couple questions though:
Why make the char* a const?
Is there any reason I shouldn't do a string instead of a character? Using character, it only outputs the first character. Using string, it outputs the whole string. I just want to make sure there isn't some good reason not to do the following:
inline GameSimulation& operator << (GameSimulation& lhs, std::string inputString)
{
//some stuff here

std::cout << inputString;

return lhs;
}
Use string, I just wrote const char* for that example, writing it as a friend function was the main point.
Gotcha, thanks for the help.

I'll avoid creating a new thread and just ask a followup question here:

Suppose I want the << operator to work with multiple data types and not just strings. Would I have to overload the function repeatedly for every operator I'd want like this;
inline GameSimulation& operator << (GameSimulation& lhs, const std::string inputString)
{
//stuff
}

inline GameSimulation& operator << (GameSimulation& lhs, const int inputInt)
{
//stuff
}


Or is there a way to make the input value something more generic? Thanks.
It depends on what you want to do in "stuff". If you want to perform operation which is defined for all types you are passing, then it is possible:
template <class T>
inline GameSimulation& operator << (GameSimulation& lhs, const T& input)
{
std::cout << input;
return lhs;
}

In this example std::cout can process with << operator both types - int and std::string, so this will work as long as std::cout has appropriate << operator.

If your types need to be processed differently, then you must create separate functions, or at least extract this different functionality in separate function (like std::cout does for << ).

Hey guys, please help me accomplish the following: I have a class called GameSimulation and I have the << operator set up so i can do
GameSimulation gameObject;
gameObject << "some text";


and the game will output that text in a specific way. Well, that works fine if i do that in main. However, how do i do the same thing from within the class?
For example, here's a function in the class definition:
void GameSimulation::setUpNewPlayer(Player *player)
{ gameWorld[player->location[0]] [player->location[1]] = player->sprite;
???? << "New player has been set up. There are now " << playerList.size() << " players online\n";
}

What do I put in place of the question marks to make it use the GameSimulation::operator << (std::string textString) method that's in the same class? Thanks for your help.

Edit: I tried 'this' and '*this' and neither worked.




(*this) << "New player has been set up. There are now " << playerList.size() << " players online\n";


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement