By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

Where to do the operator<< overloading

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.