470,586 Members | 1,368 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

Where to do the operator<< overloading

Hi all,

I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?

Cheers,

Paulo Matos

Jul 23 '05 #1
2 1894
pmatos wrote:
Hi all,

I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend.
Do you actually need that?
Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?


I'd choose the former for three reasons.

1. It simplifies dependancies. If you put all the operators in one
implementation file, that file needs to #include the headers of all the
classes that you have operator<< for.

2. If you add or change a class and the operator<< needs to be adapted, you
have to touch two files instead of one.

3. IHMO, it's a good idea if things that are declared in a header are
defined in the corresponding implementation file and not somewhere else.

Jul 23 '05 #2
pmatos wrote:

Hi all,

I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?


I would put the definition in the same file as the rest of that class.
The reason that this operator has to be a freestanding function is
more of technical nature then anything else. Logically this operator
belongs to the class.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Victor | last post: by
3 posts views Thread by Robert Wierschke | last post: by
5 posts views Thread by Ian Lazarus | last post: by
25 posts views Thread by Steve Richter | last post: by
3 posts views Thread by peak | last post: by
2 posts views Thread by Harry | last post: by
5 posts views Thread by TOMERDR | last post: by
6 posts views Thread by iLL | last post: by
2 posts views Thread by Adam Nielsen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.