Sign in to follow this  
breakspirit

C++ << operator use within class

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
[code]GameSimulation gameObject;
gameObject << "some text";[/code]

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:
[code] 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";
} [/code]
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
Try making the insertion operator a friend function.<br><br>&nbsp;[code]<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>[/code]<br><br>Edit: the forum is not formatting or something.<br>

Share this post


Link to post
Share on other sites
[quote name='NumberXaero' timestamp='1318479311' post='4872078']
Try making the insertion operator a friend function.<br><br>&nbsp;[code]<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>[/code]<br><br>Edit: the forum is not formatting or something.<br>
[/quote]

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:
[code]inline GameSimulation& operator << (GameSimulation& lhs, std::string inputString)
{
//some stuff here

std::cout << inputString;

return lhs;
}[/code]

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;
[code]inline GameSimulation& operator << (GameSimulation& lhs, const std::string inputString)
{
//stuff
}

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

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:
[code]template <class T>
inline GameSimulation& operator << (GameSimulation& lhs, const T& input)
{
std::cout << input;
return lhs;
}[/code]
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
[quote name='breakspirit' timestamp='1318477135' post='4872070']
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
[code]GameSimulation gameObject;
gameObject << "some text";[/code]

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:
[code] 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";
} [/code]
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.
[/quote]



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


L. Spiro

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this