By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,122 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,122 IT Pros & Developers. It's quick & easy.

Question on vector assignment and object equality determination

P: n/a
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)?
.......
}
}

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a


Alfonzo Morra wrote:
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 ...

Jul 23 '05 #2

P: n/a
Dear Alfonzo,

On Mon, 2005-07-11 at 09:45 +0000, Alfonzo Morra wrote:
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

Jul 23 '05 #3

P: n/a
Alfonzo Morra wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

P: n/a
Alfonzo Morra wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5

P: n/a


Alfonzo Morra wrote:
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

Jul 23 '05 #6

P: n/a


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

Jul 23 '05 #7

P: n/a


msalters wrote:

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


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

Jul 23 '05 #8

P: n/a
Alfonzo Morra wrote:


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++.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #9

P: n/a


Karl Heinz Buchegger wrote:
Alfonzo Morra wrote:

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++.


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

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.