Operator overloading and friend functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Operator overloading and friend functions

  1. #1
    Join Date
    Apr 2005
    Posts
    1,776

    Operator overloading and friend functions

    Can an overloaded operator be a friend function?

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Operator overloading and friend functions

    If the operator can be a free function it can also be a friend.

  3. #3
    Join Date
    Apr 2005
    Posts
    1,776

    Re: Operator overloading and friend functions

    Quote Originally Posted by nuzzle View Post
    If the operator can be a free function it can also be a friend.
    By free function you mean a function that is not a data member? I was talking about a data member of a class

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Operator overloading and friend functions

    Quote Originally Posted by maverick786us View Post
    By free function you mean a function that is not a data member? I was talking about a data member of a class
    A member function cannot be a friend function, it's either or. And a friend function is a free function (with special access priviliges in the class where it's declared).

    If you declare a member function to be a friend it turns into a free function.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,168

    Re: Operator overloading and friend functions

    yes, you can friend a member function:

    Code:
    class A{ public: void f(); };
    class B{ friend void A::f(); void g(); };
    
    void A::f(){ B b; b.g(); }
    in the same way, you can friend member operators.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: Operator overloading and friend functions

    To clarify: nuzzle's statement is true with respect to the same class, i.e., a member function of a given class cannot also be a friend function of that class. As you can see from superbonzo's code snippet, such a member function can be a friend function of another class. Since an overloaded operator is ultimately just a function, the same applies.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Operator overloading and friend functions

    Quote Originally Posted by superbonzo View Post
    yes, you can friend a member function:
    That doesn't compile in VS2012. It compiles only if class B makes the whole class A a friend (and not only the individual f function). But I think it should work actually so it seems like a bug.

    Anyway there are two different situations to consider and the OP will have to decide which one applies.

    I'm talking about friend functions/operators of a class, and you're talking about granting friendship to functions/operators of another class.

    I think the first usage is most common actually, see here for example,

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
    Last edited by nuzzle; May 4th, 2013 at 12:18 PM.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: Operator overloading and friend functions

    Quote Originally Posted by nuzzle
    That doesn't compile in VS2012. It compiles only if class B makes the whole class A a friend (and not only the individual f function). But I think it should work actually so it seems like a bug.
    Definitely a bug:
    Quote Originally Posted by C++11 Clause 11.3 Paragraph 5
    When a friend declaration refers to an overloaded name or operator, only the function specified by the parameter types becomes a friend. A member function of a class X can be a friend of a class Y. [ Example:
    Code:
    class Y {
      friend char* X::foo(int);
      friend X::X(char); // constructors can be friends
      friend X::~X(); // destructors can be friends
    };
    —end example ]
    or earlier:
    Quote Originally Posted by C++03 Clause 11.4 Paragraph 4
    When a friend declaration refers to an overloaded name or operator, only the function specified by the parameter types becomes a friend. A member function of a class X can be a friend of a class Y. [Example:
    Code:
    class Y {
        friend char* X::foo(int);
        // ...
    };
    —end example]
    Last edited by laserlight; May 3rd, 2013 at 01:04 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center