Click to See Complete Forum and Search --> : Question about friend function.


George2
April 13th, 2003, 08:57 AM
Hi, everyone!


I have often noticed that someone declares a friend
fucntion inside a class (in the .h file of the class)
and give the definition of the function in the .cpp file
of the same class. I want to know whether this
method is correct? Does it have some weak points? Where should
correct place which we give the definition (Note: not the declaration)
of the function?

Another question is, if I define the function like below, what is the
scope of the function? (i.e. where can compiler find the function?)

Here is the sample codes I noticed:

in A.h
--------

class A {

friend int functionA();
void memberfunction();
}
--------

A.cpp
--------
A::memberfunction()
{
//implemention
}

int functionA()
{
//implemention
}

--------


Thanks in advance,
George

Gabriel Fleseriu
April 13th, 2003, 02:29 PM
The compiler will recognize functionA as valid identifier in any translation unit that includes A.h. The function has external linkage by default.

The place where you define the function (i.e .h file vs. .cpp file) depends on whether the client code is supposed to call that function or not.

George2
April 13th, 2003, 10:47 PM
Thanks, Gabriel buddie!

What means "depends on whether the client code is supposed to call that function or not." in your reply? Can you show me an example?


regards,
George

Originally posted by Gabriel Fleseriu
The compiler will recognize functionA as valid identifier in any translation unit that includes A.h. The function has external linkage by default.

The place where you define the function (i.e .h file vs. .cpp file) depends on whether the client code is supposed to call that function or not.

George2
April 18th, 2003, 11:44 PM
Thanks, Gabriel buddy!

I have thought your comments for several days. And I
also have reference some codes by others and I find out
that almost all member function declarations are defined
in .h and almost all member function definitions are defined
in .cpp file. I still do not understand what you mean by
"the client code is supposed to call that function or not".


Can you help?


regards,
George




Originally posted by Gabriel Fleseriu
The compiler will recognize functionA as valid identifier in any translation unit that includes A.h. The function has external linkage by default.

The place where you define the function (i.e .h file vs. .cpp file) depends on whether the client code is supposed to call that function or not.

Wfranc
April 19th, 2003, 12:13 AM
May I help you, George ?

George2
April 19th, 2003, 12:23 AM
Sure Wfranc buddy!

Welcome!


regards,
George

Originally posted by Wfranc
May I help you, George ?

Wfranc
April 19th, 2003, 12:53 AM
Originally posted by George2
Sure Wfranc buddy!
Welcome!
regards,
George

The compiler will recognize functionA as valid identifier in any translation unit that includes A.h. The function has external linkage by default.
The place where you define the function (i.e .h file vs. .cpp file) depends on whether the client code is supposed to call that function or not.
Client code is the code that is supposed to be client which will call the function you want it to be called. In case, you dont want to call it, it s Okay, it turns out waste of your memory because it is in your program without being used.... But I think you must have called it so many times in your program which makes it 'worn', so if not necessary, calling it once is 100% fine...:)

Regards,
Wfranc

Wfranc
April 19th, 2003, 12:57 AM
You dont have to view my profile, because it just gives you a bunch of CodeGuru ChitChat, if you dont mind, ChitChat forum is where we can talk more :D:p:D. Now I think your problem is partially solved...Uhmm..;) :D

George2
April 19th, 2003, 01:42 AM
Thanks, Wfranc buddy!

Your reply is helpful. But can you show me a case
where the client code is not supposed to call that function
but we must define the function in .cpp file. I can not imagine
that case. :-)


regards,
George
Originally posted by Wfranc

The compiler will recognize functionA as valid identifier in any translation unit that includes A.h. The function has external linkage by default.
The place where you define the function (i.e .h file vs. .cpp file) depends on whether the client code is supposed to call that function or not.
Client code is the code that is supposed to be client which will call the function you want it to be called. In case, you dont want to call it, it s Okay, it turns out waste of your memory because it is in your program without being used.... But I think you must have called it so many times in your program which makes it 'worn', so if not necessary, calling it once is 100% fine...:)

Regards,
Wfranc

George2
April 19th, 2003, 01:44 AM
Hi, Wfranc buddy!

I silly question. How to see your profile?
//shy

regards,
George

Originally posted by Wfranc
You dont have to view my profile, because it just gives you a bunch of CodeGuru ChitChat, if you dont mind, ChitChat forum is where we can talk more :D:p:D. Now I think your problem is partially solved...Uhmm..;) :D

Wfranc
April 22nd, 2003, 05:24 AM
In most cases the client code is supposed to do that...:), just like I am supposing to answer your thread now...:D. Just because I dont exactly know what you are trying to do, so I now cant figure out an easy example to explain for that...:)
How to see your profile?
This is not a silly question... It is also difficult for me . how could I see what you were doing at that time, I just........guess...:( :(
So... problem solved ?

George2
April 22nd, 2003, 06:21 AM
Thanks, Wfranc buddy!

My question arises from reading Bjarne Stroustrup's famous
book -- The C++ Programming Language Special Edition. In this
book it deals with where should we define a friend class and
a friend function. But my purpose is only to use the friend function. But in this book it is said that the compiler has some
method to find a friend function. So you must define a friend function in a proper place or else the compiler can not find it. Since a friend function does not belongs to a class, so it is not easy to find. (This is not Bjarne's opinion but mine.)

Then I begin to read some source codes of the buddies of
out lab. I find they all define the friend function in the .cpp file.
And give the declaration in the .h file.

I simply want to know whether this method will make compiler
working more efficient or the contrary? What is the correct method to declare and define the friend function of a class?

Can you help?


regards,
George

Originally posted by Wfranc
In most cases the client code is supposed to do that...:), just like I am supposing to answer your thread now...:D. Just because I dont exactly know what you are trying to do, so I now cant figure out an easy example to explain for that...:)
How to see your profile?
This is not a silly question... It is also difficult for me . how could I see what you were doing at that time, I just........guess...:( :(
So... problem solved ?

Gabriel Fleseriu
April 22nd, 2003, 07:36 AM
Originally posted by George2
...Since a friend function does not belongs to a class, so it is not easy to find. (This is not Bjarne's opinion but mine.)


Actually, a friend function does belong to the class. Maybe not in the way you expect it, but it does. Herb Sutter has a very good discussion about this in his books ("Exceptional C++" and "More Exceptional C++"). Ask yourself following question: what is the difference between a friend function and a static member function? Think about it...;)

By me:
The place where you define the function (i.e .h file vs. .cpp file) depends on whether the client code is supposed to call that function or not.


Sorry, but re-reading this statement I must say that it is not correct. The client code will be able to call the friend function in both cases. The only difference is that the client code will need recompilation if the implementation of the friend function changes if this is defined in the header file. OTOH, if you want to make the function inline, you must define it in the header. However, this is rarely the case, so you normally place function definitions in the cpp file (not only friends, but all definitions). This reduces the compilation coupling between your translation units.

HTH,

George2
April 22nd, 2003, 07:46 AM
Thanks, Gabriel buddy!

I think a friend function and static function are not the
same. :-) Since a friend function can be a friend of several
classes but a static function of a class only belongs to the
owner class. So the compiler must have some method to
find the friend function. As Bjarne said, "declared in the immediately enclosing scope".

regards,
George

Originally posted by Gabriel Fleseriu
Actually, a friend function does belong to the class. Maybe not in the way you expect it, but it does. Herb Sutter has a very good discussion about this in his books ("Exceptional C++" and "More Exceptional C++"). Ask yourself following question: what is the difference between a friend function and a static member function? Think about it...;)



Sorry, but re-reading this statement I must say that it is not correct. The client code will be able to call the friend function in both cases. The only difference is that the client code will need recompilation if the implementation of the friend function changes if this is defined in the header file. OTOH, if you want to make the function inline, you must define it in the header. However, this is rarely the case, so you normally place function definitions in the cpp file (not only friends, but all definitions). This reduces the compilation coupling between your translation units.

HTH,

Gabriel Fleseriu
April 22nd, 2003, 07:54 AM
Originally posted by George2
I think a friend function and static function are not the
same. :-) Since a friend function can be a friend of several
classes but a static function of a class only belongs to the
owner class. So the compiler must have some method to
find the friend function. As Bjarne said, "declared in the immediately enclosing scope".

It was never my intention to say they were the same. They are definitively not. The one difference you named is correct. There also are others, for example class access qualifiers(public/protected/private) don't apply to friends, and friends are not inherited. To ask the same question the other way around: what are the similarities between a friend function and a static member function? Or, more generally, why can we say that a friend function is actually part of the class? What is a class, actually? Try to think of these questions not from the point of view of the language syntax, but from that of what a class is supposed to model.

George2
April 22nd, 2003, 07:56 AM
Thanks for all of your help, Gabriel buddy!

regards,
George

Originally posted by Gabriel Fleseriu
It was never my intention to say they were the same. They are definitively not. The one difference you named is correct. There also are others, for example class access qualifiers(public/protected/private) don't apply to friends, and friends are not inherited. To ask the same question the other way around: what are the similarities between a friend function and a static member function? Or, more generally, why can we say that a friend function is actually part of the class? What is a class, actually? Try to think of these questions not from the point of view of the language syntax, but from that of what a class is supposed to model.