Jump to content
  • Advertisement
Sign in to follow this  
breakspirit

C++ << operator use within class

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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>

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!