-
May 3rd, 2013, 05:22 AM
#1
Operator overloading and friend functions
Can an overloaded operator be a friend function?
-
May 3rd, 2013, 05:36 AM
#2
Re: Operator overloading and friend functions
If the operator can be a free function it can also be a friend.
-
May 3rd, 2013, 06:11 AM
#3
Re: Operator overloading and friend functions
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
-
May 3rd, 2013, 07:16 AM
#4
Re: Operator overloading and friend functions
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.
-
May 3rd, 2013, 08:35 AM
#5
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.
-
May 3rd, 2013, 09:17 AM
#6
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.
-
May 3rd, 2013, 11:56 AM
#7
Re: Operator overloading and friend functions
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
Last edited by nuzzle; May 4th, 2013 at 11:18 AM.
-
May 3rd, 2013, 12:02 PM
#8
Re: Operator overloading and friend functions
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:
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:
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 12:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|