473,287 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 2028
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I hope they can use << to output their different...
3
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
5
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler...
25
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
3
by: peak | last post by:
I have this code that I am using to create a heap template <class t> void BinaryHeap<t>::insert( const T &x) { int hole = ++ currentSize; for ( ; hole > 1 && x < array;hole /= 2) arrsy = arrsy;...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
5
by: TOMERDR | last post by:
Hi, I was requested to write a code similar to this: CArchive ar; //Not mfc CArchive..... .... void * vp = &XXX; // any object. ar << vp, sizeof(XXX);
6
by: iLL | last post by:
Okay, I’m just a little confused on exactly what the system is doing when I say: #include <iostream> class test { private: int i; public:
2
by: Adam Nielsen | last post by:
Hi everyone, Following advice previously given in this group, I've created a function that I'm using to "inline" the creation of a string. Regardless of what you think of my method, I'm...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.