CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about friend function.

    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

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    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.

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    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.

  5. #5
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Unhappy

    May I help you, George ?

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    Sure Wfranc buddy!

    Welcome!


    regards,
    George

    Originally posted by Wfranc
    May I help you, George ?

  7. #7
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Unhappy George...

    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

  8. #8
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Unhappy George...

    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 . Now I think your problem is partially solved...Uhmm..

  9. #9
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: George...

    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

  10. #10
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking Re: George...

    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 . Now I think your problem is partially solved...Uhmm..

  11. #11
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Unhappy George

    In most cases the client code is supposed to do that..., just like I am supposing to answer your thread now.... 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...
    Code:
    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 ?

  12. #12
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Re: George

    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.... 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...
    Code:
    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 ?

  13. #13
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Re: George

    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,
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  14. #14
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: Re: Re: George

    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,

  15. #15
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Re: Re: Re: George

    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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured