Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Darkor

Overloading the << and >> operators for classes in namespaces

This topic is 5552 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

As the title reads, I have encountered problems when I overload the << and >> for classes in a namespace. It''s not actually a problem because what I wanted to do is to make the overloading function a friend of the class so that it is able to access its private parameters. The only workaround that I found was to make the function merely global (and outside the namespace too). Then use the accessor functions to do whatever I want with the attributes. But is there a way to retain the benefits of being a friend function?

Share this post


Link to post
Share on other sites
Advertisement
I''m using VS.net. But really, is the occurence of this problem that small?

I mean, I don''t overload << and >> for classes in namespaces all the time but someone must have done it.

The problem is solved really just that the overloaded functions don''t have private access to the classes'' members.

Anyway my thoughts are that since you are overloading one of the standard operators, it can''t exist within another namespace, otherwise you are defining a totally new version and not overloading the standard operators.

Share this post


Link to post
Share on other sites
Oh I forgot to mention that I was overloading the << and >> operators to take standard input and output streams as left hand parameters.

Otherwise the code is similar.

Share this post


Link to post
Share on other sites
Well, that''s the right way to do it. What problems are you having?


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I don't quite understand your problem. You would generally do one of these two:

namespace foo
{
class bar
{
public:
bar() : _m("Test") {}
...
friend std::ostream& operator<<( std::ostream& lhs, const bar& rhs );
...
private:
...
std::string _m;
...
};

...

std::ostream& operator<<( std::ostream& lhs, const bar& rhs )
{
lhs << rhs._m << ...;
... invoke any more std::ostream methods (on "lhs"), etc, etc
return lhs;
}
} // namespace foo


   
namespace foo
{
class bar
{
public:
bar() : _m("Test") {}
...
std::ostream& print( std::ostream& os ) const;
...
private:
...
std::string _m;
...
};

...

inline std::ostream& operator<<( std::ostream& lhs, const bar& rhs ) // I take it you think that you won't be able to invoke foo::operator<<(), but have you even tried it? It should work.

{
return rhs.print(lhs);
}

...

std::ostream& bar::print( std::ostream& os ) const
{
os << _m << ...;
... invoke any more std::ostream methods (on "os"), etc, etc
return os;
}
} // namespace foo


These should compile and run correctly.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 11, 2003 11:17:49 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!