Archived

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

Darkor

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

Recommended Posts

Darkor    134
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
Darkor    134
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
dalleboy    324
Are you doing something like this?

namespace foo
{
class bar
{
friend bar& operator<<(bar& lhs, const bar& rhs);

private:
int a;
int b;
};

bar& operator<<(bar& lhs, const bar& rhs)
{
lhs.a = rhs.b;
lhs.b = 47;
return lhs;
}
}

void baz()
{
foo::bar a;
foo::bar b;
a << b;
}


[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

Share this post


Link to post
Share on other sites
Darkor    134
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
Sneftel    1788
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
dalleboy    324

namespace foo
{
class bar
{
friend std::ostream& operator<<(std::ostream& os, const bar& rhs);
private:
int a;
int b;
};
std::ostream& operator<<(std::ostream& os, const bar& rhs)
{
return os << "a=" << a << "b=" << b;
}
}
void baz()
{
std::ostream os = /*...*/;
foo::bar a;
os << a;
}


[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

Share this post


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