473,382 Members | 1,775 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,382 software developers and data experts.

where to declare a friend operator >> (as well as >>)... in .h or.cpp file


Say we have this structure:

Struct Foo{
....
friend ostream& operator << (ostream& s, Foo & m);
.....
};

friend ostream& operator << (ostream& s, Foo & m){

//access and print
}
I had a multiple inclusion issue when I placed the definition of
operator << in .h file despite include guards. So, I eventually moved
it to Foo.cpp class.

What is the suggested design ?
Thanks
Sep 7 '08 #1
6 1903
If you place a function definition in a header file use the word 'inline',
unless it's defined within a class definition.
Umm, why inline is necessary in my case?

I only defined once in only one .h file, which obviously has include
guards. However, I have many other files, in the same project, that
include this .h file.


Sep 7 '08 #2
On Sep 7, 7:18 pm, "Alf P. Steinbach" <al...@start.nowrote:
* puzzlecracker:
If you place a function definition in a header file use the word 'inline',
unless it's defined within a class definition.
Umm, why inline is necessary in my case?
I only defined once in only one .h file, which obviously has include
guards. However, I have many other files, in the same project, that
include this .h file.

The include guard only prevents multiple includes in each compilation unit.

When you have multiple compilation units you have one (effective) include in each.

Thus one definition in each.

And when you have multiple definitions you need 'inline'.
Argh, got it, I like this subtle
Except if it's a template.
What is the rule with template in regard to putting the template
function in .h file.

Another question: what is the best design for opereraton<< design?

ty
Sep 7 '08 #3
On Sep 7, 4:42 pm, puzzlecracker <ironsel2...@gmail.comwrote:
What is the rule with template in regard to putting the template
function in .h file?
I think you have to put a template function in a .h file if you plan
to use it in multiple compilation units. I've never heard of a system
that could handle template instantiation at link-time, though I heard
that Microsoft handles inline functions at link-time now, so maybe
template functions too? On the other hand, such code wouldn't be
portable so you probably don't want to use the feature even if it does
exist.
Another question: what is the best design for opereraton<< design?
I don't know. Here's what I do:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4. virtual std::ostream& operator>>(std::ostream& o) const { return o <<
  5. "A's data";}
  6. };
  7.  
  8. std::ostream& operator<<( std::ostream& o, const A& a)
  9. {
  10. return a > o;
  11. }
  12.  
Sep 8 '08 #4
On Sep 8, 6:16 am, mqrk <McKenna.Mar...@gmail.comwrote:
On Sep 7, 4:42 pm, puzzlecracker <ironsel2...@gmail.com>
wrote:What is the rule with template in regard to putting
the template
function in .h file?
I think you have to put a template function in a .h file if
you plan to use it in multiple compilation units. I've never
heard of a system that could handle template instantiation at
link-time,
Sun CC. Comeau C++.

Probably most Unix compilers, for that matter, except g++.
Traditionally, templates were instantiated at link time, and
most Unix vendor compilers try to support this, for reasons of
backwards compatibility, if for nothing else.

What is special is that you *can* put template and inline
functions in a header file, without incuring an error.
though I heard that Microsoft handles inline functions at
link-time now, so maybe template functions too? On the other
hand, such code wouldn't be portable so you probably don't
want to use the feature even if it does exist.
Another question: what is the best design for opereraton<< design?
I don't know. Here's what I do:
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4. virtual std::ostream& operator>>(std::ostream& o) const { return o <<
  5. "A's data";}
  6. };
Expand|Select|Wrap|Line Numbers
  1.  
  2.         
  3.                 std::ostream& operator<<( std::ostream& o, const A& a)
  4. {
  5. return a > o;}
  6.  
  7.  
  8.         
  9.  
  10.  
  11.  
Help. That's really confusing. I usually provide a normal,
named member function print, and derive from a template class
using the Barton and Nackman trick to provide the operators, but
otherwise, there's no problem with using a friend, as long as
you define it in a normal source file, and not in the header.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 8 '08 #5
I usually provide a normal,
named member function print, and derive from a template class
using the Barton and Nackman trick to provide the operators, but

Please elaborate on this:the Barton and Nackman trick

Sep 8 '08 #6
On Sep 8, 3:26 pm, puzzlecracker <ironsel2...@gmail.comwrote:
I usually provide a normal,
named member function print, and derive from a template
class using the Barton and Nackman trick to provide the
operators, but
Please elaborate on this:the Barton and Nackman trick
It's pretty straight-forward, really. The base class contains
an inline definition of the operator---declared as friend,
because that's the only way you can provide an implementation
inline in a class for a non-member function. It's not really
something fundamentally necessary for operator<< and >>; it's
more useful for things like an operator+ (based on +=), etc.
Anyway, you define a base class template along the lines of:

template< typename T >
class Operators
{
friend T operator+( T const& lhs, T const& rhs )
{
T result( lhs ) ;
result += rhs ;
return result ;
}
// ...
friend std::ostream& operator<<(
std::ostream const& dest,
T const& obj )
{
obj.print( dest ) ;
return dest ;
}
// ...
} ;

Then, whenever you need the operators, just derive from the
class:

class Whatever : public Operators< Whatever >
{
public :
// ...
Whatever& operator+=( Whatever const& other ) ;
// ...
void print( std::ostream& dest ) const ;
// ...
} ;

I actually have several different template base classes for
this: ArithmeticOperators, MixedArithmeticOperators (with two
template parameters, for mixed type arithmetic),
STLIteratorOpertors (converts a "normal" iterator into STL), and
IOStreamOperators.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 8 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Oplec | last post by:
Hi, I am trying to figure out the correct syntax for declaring a friend function in a class template and then defining that member after the class. The code example below is what I am trying to get...
2
by: cppaddict | last post by:
Hi, The question of where to define operator< came up when I was using the std::sort method. Let's say I have a container whose members (of class A) I need to sort. In particular, we have some...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
3
by: Patrick Labatut | last post by:
The following code should compile fine with g++ 3.3 and 3.4. However if I instead put the friend operator* declaration right below the operator* member, g++ 3.4 won't compile it anymore. Could...
3
by: steve | last post by:
There is a curious type of operator overloading with streams that takes the form of: friend ostream& operator<<(ostream &stream, String out); Is it possible to define a similar type of friend...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
2
by: 海风 | last post by:
Normally, when i define a friend operator + the argument used by the operator function is references. but i try to use pointer and look like this: class A; friend A operator+( A *a1, A *a2); I...
5
by: noone | last post by:
hi. I don't use exceptions much in the embedded world, but for my plugin interface to a hardware MPEG encoder I'd like to, since there are so many places that the crummy kernel driver can do bad...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.