Question on vector assignment and object equality determination

A

Alfonzo Morra

Hi,

If I have code like this :

class MyClass {
std::vector< int> arr_ ;
std::vector < std::pair < long, double> > adj ;
......
MyClass( const MyClass& rhs) {
......
this->arr_ = rhs.arr_ ; //will the elements get copied accross
//into this->arr ?
this->adj = rhs.adj ; //more complicated vector, do elements
//(pairs) get copiedinto this->adj ?
.....
}

MyClass& operator=( const MyClass& rhs) {
if( this != rhs ) { // <- compiler barfs here
// how can I check to make sure !(same object)?
.......
}
}
 
A

Alfonzo Morra

Alfonzo said:
Hi,

If I have code like this :

class MyClass {
std::vector< int> arr_ ;
std::vector < std::pair < long, double> > adj ;
......
MyClass( const MyClass& rhs) {
......
this->arr_ = rhs.arr_ ; //will the elements get copied accross
//into this->arr ?
this->adj = rhs.adj ; //more complicated vector, do elements
//(pairs) get copiedinto this->adj ?
.....
}

MyClass& operator=( const MyClass& rhs) {
if( this != rhs ) { // <- compiler barfs here
// how can I check to make sure !(same object)?
.......
}
}
Ok, I realized (in the case of the copy assignment constructor), that I
have to pass the address of the reference (i.e. &rhs). I find this
slightly baffling as I thought a reference is synonymous with a pointer.
In which case, &(rhs) will be returning the adress of the pointer itself
and not the address that the pointer was pointing to. Further
elucidation please ...
 
C

Chris Dams

Dear Alfonzo,

Ok, I realized (in the case of the copy assignment constructor), that I
have to pass the address of the reference (i.e. &rhs). I find this
slightly baffling as I thought a reference is synonymous with a pointer.
In which case, &(rhs) will be returning the adress of the pointer itself
and not the address that the pointer was pointing to. Further
elucidation please ...

Why not pass the reference and take the address inside the function?
Much more readable and much more usual. A point of the new notation
"rhs" (without &) in case of references to obtain the object instead of
the address is that it makes operator overloading readable. Imaginge
that you would have a complex number class and you had to take the
address when passing comlex numers. That would be &c1+&c2. Ugh.

Best wishes,
Chris
 
K

Karl Heinz Buchegger

Alfonzo said:
Hi,

If I have code like this :

class MyClass {
std::vector< int> arr_ ;
std::vector < std::pair < long, double> > adj ;
......
MyClass( const MyClass& rhs) {
......
this->arr_ = rhs.arr_ ; //will the elements get copied accross
//into this->arr ?

Yes. A vector assigns everything thats in it.
this->adj = rhs.adj ; //more complicated vector, do elements
//(pairs) get copiedinto this->adj ?
.....

Same here.
 
K

Karl Heinz Buchegger

Alfonzo said:
Ok, I realized (in the case of the copy assignment constructor), that I
have to pass the address of the reference (i.e. &rhs). I find this
slightly baffling as I thought a reference is synonymous with a pointer.

And you found out, that is it not.
A reference is another name for an existing object. Nothing more, nothing less.
In some cases a reference is internally implemented by the compiler by using
a pointer under the hood. But a reference *is not* a pointer.
 
A

Alfonzo Morra

Alfonzo said:
Hi,

If I have code like this :

class MyClass {
std::vector< int> arr_ ;
std::vector < std::pair < long, double> > adj ;
......
MyClass( const MyClass& rhs) {
......
this->arr_ = rhs.arr_ ; //will the elements get copied accross
//into this->arr ?
this->adj = rhs.adj ; //more complicated vector, do elements
//(pairs) get copiedinto this->adj ?
.....
}

MyClass& operator=( const MyClass& rhs) {
if( this != rhs ) { // <- compiler barfs here
// how can I check to make sure !(same object)?
.......
}
}

Thanks Karl
 
M

msalters

Alfonzo Morra schreef:
Hi,

If I have code like this :
MyClass& operator=( const MyClass& rhs) {
if( this != rhs ) { // <- compiler barfs here
// how can I check to make sure !(same object)?
.......
}
}

The quick solution is &rhs, but that's probably now what you want.
The problem is that you still need to make the ..... part
exception-safe,
and if you do, you don't need the this==&rhs check.

E.g. one solution is the canonical assignment:
MyClass& operator=( const MyClass& rhs) {
MyClass tmp (rhs); // copy
this->swap(tmp); // now *this==rhs
return *this; // tmp.~MyClass destorys old value
}
Now, if you happen to have a self-assignment, it simply swaps two
identical values.

HTH,
Michiel Salters
 
A

Alfonzo Morra

msalters said:
Alfonzo Morra schreef:



The quick solution is &rhs, but that's probably now what you want.
The problem is that you still need to make the ..... part
exception-safe,
and if you do, you don't need the this==&rhs check.

E.g. one solution is the canonical assignment:
MyClass& operator=( const MyClass& rhs) {
MyClass tmp (rhs); // copy
this->swap(tmp); // now *this==rhs
return *this; // tmp.~MyClass destorys old value
}
Now, if you happen to have a self-assignment, it simply swaps two
identical values.

HTH,
Michiel Salters

Hi Michiel,


This looks more like what I'm trying to do. Could you please give me an
example of the implementation of the swap() function ?

Thanks
 
K

Karl Heinz Buchegger

Alfonzo said:
This looks more like what I'm trying to do. Could you please give me an
example of the implementation of the swap() function ?

Aehm. Swap the content of 2 variables.
Shouldn't be a big problem for any decent programmer.

void swap( int& a, int& b )
{
int tmp( a );
a = b;
b = tmp;
}

This is so straight forward, that the STL contains a template for it.
(Now modify the above, that it becomes a member function and you have it)

Seriously: You should get some literature. You won't get very far in
your C++ studies without one. 'Try and error' simply doesn't work in a
language as complex as C++.
 
A

Alfonzo Morra

Karl said:
Aehm. Swap the content of 2 variables.
Shouldn't be a big problem for any decent programmer.

void swap( int& a, int& b )
{
int tmp( a );
a = b;
b = tmp;
}

This is so straight forward, that the STL contains a template for it.
(Now modify the above, that it becomes a member function and you have it)

Seriously: You should get some literature. You won't get very far in
your C++ studies without one. 'Try and error' simply doesn't work in a
language as complex as C++.

LOL. I *know* how to swap variables, I just wanted to make sure I was
not jumping to conclusions before "rolling my own".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top