Where to do the operator<< overloading

P

pmatos

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
 
K

Karl Heinz Buchegger

pmatos said:
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.
 
R

Rolf Magnus

pmatos said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top