473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Why can't '='(Assignment) operator be overloaded as friend function ?

Thanx in advance for the response...

I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.

Why can't the = (assignment) operator be overloaded as a friend
function ?
I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it
said the following :

The operators [] , -> , = cannot be overloaded as static functions

from which i figured out that since 'friend' qualifier makes the
function a static function( i.e. a per-class member ), hence =
operator can't be overloaded as a friend function.But why is such
restrction there in the language in the first place ???
Jul 19 '05 #1
8 17835
> 1) If you do not define an assignment operator in your class
the compiler will create a default one for you.


Sorry I did not finish that one:
The created default operator definition
will result in an ambigous call whenever
you'll use the assignment

ex:
class A {

public:
A(int i) : _i(i) {}

friend A& operator=(A& lhs, const A& rhs);

private:
int i;
};
A& operator=(A& lhs, const A& rhs) {
lhs._i = rhs._i;
}

int main() {
A a(0), b(10);

a = b;
}
leads to the following error msg: (GCC 3.2, Linux Mandrake 9.0, Intel
Pentium):

See the *** line

launcher.C:11: `A& operator=(A&, const A&)' must be a nonstatic member
function
launcher.C:18: `A& operator=(A&, const A&)' must be a nonstatic member
function

launcher.C: In function `int main()':
launcher.C:25: ambiguous overload for `A& = A&' operator ***
launcher.C:6: candidates are: A& A::operator=(const A&)
launcher.C:18: A& operator=(A&, const A&)
launcher.C:18: A& operator=(A&, const A&)
--
Jan Rendek
INRIA, Lorraine
r e n d e k @ l o r i a . f r

Jul 19 '05 #2

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Thanx in advance for the response...

I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.

Why can't the = (assignment) operator be overloaded as a friend
function ?
I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it
said the following :

The operators [] , -> , = cannot be overloaded as static functions

from which i figured out that since 'friend' qualifier makes the
function a static function( i.e. a per-class member ), hence =
operator can't be overloaded as a friend function.But why is such
restrction there in the language in the first place ???


Different rules apply to the functions which are called because they are
member functions or friend functions. For instance

class X // uses friend
{
public:
X(int); // note we can make an X from an int
friend bool operator<(const X&, const X&);
};

Given the above

X x;
if (1 < x)
...

is legal. The compiler will automatically construct an X object from the int
1. But given
class Y // uses member
{
public:
Y(int); // note we can make an Y from an int
bool operator<(const Y&) const;
};

then

Y y;
if (1 < y)
...

is not legal. With a member function you do not get the automatic conversion
of the first argument, the compiler will not construct a Y object from an
int in the expression 1 < y.

You can probably guess what is coming now. If you were able to declare
operator= as a friend like this

class X // uses friend
{
public:
X(int); // note we can make an X from an int
X& operator=(X&, const X&);
};

then stupid code like this

X x;
1 = x;

would be legal!

In short requiring that operator= be a member function ensures that what you
are assigning to really is a bona fide object, not some temporary
constructed by the compiler.

john
Jul 19 '05 #3
>
You can probably guess what is coming now. If you were able to declare
operator= as a friend like this

class X // uses friend
{
public:
X(int); // note we can make an X from an int
X& operator=(X&, const X&);
};


Of course I meant

friend X& operator=(X&, const X&);

john
Jul 19 '05 #4

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message news:17**************************@posting.google.c om...
Why can't the = (assignment) operator be overloaded as a friend
function ?


The copy assignment operator is generated by the compiler if you don't define
one for a class. If you try to implement it as a non-member function, it doesn't
supress the generated one.
Jul 19 '05 #5
John Harrison <jo*************@hotmail.com> wrote in message
news:be************@ID-196037.news.uni-berlin.de...

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Thanx in advance for the response...

I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.

Why can't the = (assignment) operator be overloaded as a friend
function ?
I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it
said the following :

The operators [] , -> , = cannot be overloaded as static functions

from which i figured out that since 'friend' qualifier makes the
function a static function( i.e. a per-class member ), hence =
operator can't be overloaded as a friend function.But why is such
restrction there in the language in the first place ???
Different rules apply to the functions which are called because they are
member functions or friend functions. For instance

class X // uses friend
{
public:
X(int); // note we can make an X from an int
friend bool operator<(const X&, const X&);
};

Given the above

X x;
if (1 < x)
...

is legal. The compiler will automatically construct an X object from the

int 1. But given
class Y // uses member
{
public:
Y(int); // note we can make an Y from an int
bool operator<(const Y&) const;
};

then

Y y;
if (1 < y)
...

is not legal. With a member function you do not get the automatic conversion of the first argument, the compiler will not construct a Y object from an
int in the expression 1 < y.

You can probably guess what is coming now. If you were able to declare
operator= as a friend like this

class X // uses friend
{
public:
X(int); // note we can make an X from an int
X& operator=(X&, const X&);
};

then stupid code like this

X x;
1 = x;

would be legal!

In short requiring that operator= be a member function ensures that what you are assigning to really is a bona fide object, not some temporary
constructed by the compiler.
Wouldn't the above fail to compile? If the lhs is a temporary, it wouldn't
bind to the non-const-reference first parameter of the operator=.

Cheers,

Stuart.
john

Jul 19 '05 #6
Nitin Bhardwaj wrote:
...
Why can't the = (assignment) operator be overloaded as a friend
function ?
I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it
said the following :

The operators [] , -> , = cannot be overloaded as static functions

from which i figured out that since 'friend' qualifier makes the
function a static function( i.e. a per-class member ), hence =
operator can't be overloaded as a friend function.But why is such
restrction there in the language in the first place ???


I guess the real question that was meant by the above wording is why
copy assignment operator cannot be overloaded by a standalone
(non-member) function.

The short answer is because the C++ standard does not allow it. The
longer answer should probably include the rationale that lead to this
restriction.

The rationale is as follows. Since the compiler always provides an
implicit declaration of copy assignment operator for a class that
doesn't declare one explicitly, later declaration of standalone copy
assignment operator would change the meaning of the assignment in the
middle of the translation unit:

class A {
/* no assignment operator explicitly declared */
};

void foo() {
A a, b;
a = b; // this invokes the implicitly declared assignment
}

A& operator =(A& lhs, const A& rhs);

void bar() {
A a, b;
a = b; // this invokes the user-declared assignment
}

Note that moving the definition of function 'bar' to some point above
the declaration of the assignment operator would change its meaning.
This behavior was considered to be potentially dangerous (and it is).

For this reason, the copy assignment operator is not permitted to be
overloaded by a standalone function in C++.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #7
Hello
The reason why a = assignment operator function can't be static is , when u
right that , u need to have a this pointer for reason one that u need to
elimite the possiblibity of self assignment
for this u need a this pointer and this pointer is not passed by default in
static fuinctions

WBr
Vijay

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Thanx in advance for the response...

I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.

Why can't the = (assignment) operator be overloaded as a friend
function ?
I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it
said the following :

The operators [] , -> , = cannot be overloaded as static functions

from which i figured out that since 'friend' qualifier makes the
function a static function( i.e. a per-class member ), hence =
operator can't be overloaded as a friend function.But why is such
restrction there in the language in the first place ???

Jul 19 '05 #8
vijay wrote:
...
The reason why a = assignment operator function can't be static is , when u
right that , u need to have a this pointer for reason one that u need to
elimite the possiblibity of self assignment
for this u need a this pointer and this pointer is not passed by default in
static fuinctions
...


There is absolutely no need to have a 'this' pointer to eliminate
self-assignment. As long as you can obtain and compare addresses of
actual lhs and rhs objects, you can easily detect and eliminate
self-assignment.

It has nothing to do with 'this' pointer.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: - Steve - | last post by:
If you want to see all the code it's at http://planetevans.com/c However I think I have all the relevant parts here. main() makes the following call IntArray c = a + b; // IntArray is my...
8
by: Clifton M. Bean | last post by:
First, I defined three classes (listed below): =========== // 1st class =========== class PointCl { public: PointCl & operator= (const PointCl & rgh ); //define as usual assingment operator
2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
9
by: August1 | last post by:
Below are the declaration for an overloaded assignment operator in an interface file in addition to the implementation file definition of the function. What would be an appropriate if condition to...
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.