# C++ << operator use within class

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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>

##### Share on other sites

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; }

##### Share on other sites
Use string, I just wrote const char* for that example, writing it as a friend function was the main point.

##### Share on other sites
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.

##### Share on other sites
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 << ).

##### Share on other sites
Hidden

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

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 47
• ### Forum Statistics

• Total Topics
632636
• Total Posts
3007574
• ### Who's Online (See full list)

There are no registered users currently online

×