Operator overloading and friend functions
Can an overloaded operator be a friend function?
Re: Operator overloading and friend functions
If the operator can be a free function it can also be a friend.
Re: Operator overloading and friend functions
Quote:
Originally Posted by
nuzzle
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
Re: Operator overloading and friend functions
Quote:
Originally Posted by
maverick786us
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.
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.
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.
Re: Operator overloading and friend functions
Quote:
Originally Posted by
superbonzo
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
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]