473,386 Members | 1,644 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,386 software developers and data experts.

Whats wrong with this copy assignment cstor ?

I've the ff code in cp assignmenent cstor:

PB& PB::operator=( const PB& b) {
if ( this != &b ) {
PB *pb = new PB( b ) ;
this = pb ; // <- Compiler barfs here
}
return *this ;
}

The error msg is : "'=' : left operand must be l-value". I seem to be
getting my references and pointers mixed up. if (this) is a pointer,
what is it pointing to ?. I rember reading somewhere that the (this)
pointer points to the current instance of the class or the "live"
object). If this indeed the case, then I see no reason why I cannot
assign (this) to another valid pointer.

Could someone please point out the correct way to implement this cp
assignment cstor?. Tks

Al
Jul 23 '05 #1
4 2230


Alfonzo Morra wrote:
I've the ff code in cp assignmenent cstor:

PB& PB::operator=( const PB& b) {
if ( this != &b ) {
PB *pb = new PB( b ) ;
this = pb ; // <- Compiler barfs here
}
return *this ;
}

The error msg is : "'=' : left operand must be l-value". I seem to be
getting my references and pointers mixed up. if (this) is a pointer,
what is it pointing to ?. I rember reading somewhere that the (this)
pointer points to the current instance of the class or the "live"
object). If this indeed the case, then I see no reason why I cannot
assign (this) to another valid pointer.

Could someone please point out the correct way to implement this cp
assignment cstor?. Tks

Al

I found out (I think) why I can't assign to the this pointer, it's
because it's a const pointer. Hmmm ... so I can't call the copy
constructor as I'm doing above, I'll have to reproduce the cody in my
copy constructor ?

That dosen't sound very clean to me. I'm sure there must be a cleaner,
more OOP way of doing this ...

Jul 23 '05 #2
Alfonzo Morra wrote:
I've the ff code in cp assignmenent cstor:

PB& PB::operator=( const PB& b) {
if ( this != &b ) {
PB *pb = new PB( b ) ;
this = pb ; // <- Compiler barfs here
Even if the compiler lets you do the above it wouldn't work anyway. The
"this" pointer is a LOCAL variable and so changing its value has no effect
at all.
}
return *this ;
}

Jul 23 '05 #3
Alfonzo Morra wrote:
I've the ff code in cp assignmenent cstor:

PB& PB::operator=( const PB& b) {
if ( this != &b ) {
PB *pb = new PB( b ) ;
this = pb ; // <- Compiler barfs here
}
return *this ;
}

The error msg is : "'=' : left operand must be l-value".
You can't assign to 'this'. Objects cannot change their
address. (You can move an object, but that's not what's
going on here).

Imagine what would happen here, if objects could change address:

void foo()
{
PB pb, qb;
pb = qb;
}

When the function ends, the compiler will destroy pb. But if
your operator= has relocated the object to a new address, the
compiler will delete the old address.

Could someone please point out the correct way to implement this cp
assignment cstor?


It looks like implementing it in terms of the copy-constructor
isn't going to work. One solution is to move your code from the
copy-constructor to the assignment-operator, and then you can
have the copy-constructor call the assignment-operator.

Jul 23 '05 #4
In message <11*********************@f14g2000cwb.googlegroups. com>, Old
Wolf <ol*****@inspire.net.nz> writes
Alfonzo Morra wrote:
I've the ff code in cp assignmenent cstor:

PB& PB::operator=( const PB& b) {
if ( this != &b ) {
PB *pb = new PB( b ) ;
this = pb ; // <- Compiler barfs here
}
return *this ;
}

The error msg is : "'=' : left operand must be l-value".


You can't assign to 'this'. Objects cannot change their
address. (You can move an object, but that's not what's
going on here).

Imagine what would happen here, if objects could change address:

void foo()
{
PB pb, qb;
pb = qb;
}

When the function ends, the compiler will destroy pb. But if
your operator= has relocated the object to a new address, the
compiler will delete the old address.

Could someone please point out the correct way to implement this cp
assignment cstor?


It looks like implementing it in terms of the copy-constructor
isn't going to work. One solution is to move your code from the
copy-constructor to the assignment-operator, and then you can
have the copy-constructor call the assignment-operator.

But the assignment operator has (in principle) to deal with deleting the
old values before assigning the new ones, which the copy constructor
doesn't. That can cause complications.

It's often better to write a swap() function and then use the
copy-and-swap idiom to implement the assignment operator in terms of the
copy constructor.
PB & PB::operator=(PB const & a)
{
PB(a).swap(*this);
return *this;
}

so the temporary's destructor will deal with deleting the old values.

--
Richard Herring
Jul 23 '05 #5

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

Similar topics

20
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as...
17
by: Michael Sparks | last post by:
In this code, the temporary inside_t instantiated in main() goes out of scope by the time we hit the next line. MSVC 2003 doesn't complain about that even at the highest warning level. Is it...
6
by: Birt | last post by:
to only initialize part of members? class A { int m1; int m2; int m3 public: A(v2, v3) : m2(v2), m3(v3) {} };
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
6
by: Simon Mansfield | last post by:
Im trying to make a C program that takes in a date (birthday) and tells the user how many days it has been since that date. So far I have got this... It compiles ok but then crashes, with no idea...
2
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for...
16
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.