473,385 Members | 1,766 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,385 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 2035
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.