Question on vector assignment and object equality determnation

A

Alfonzo Morra

Hi,

If I have code like this :

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

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

dennis

MyClass( const MyClass& rhs) {
......
this->arr_ = rhs.arr ; //will the elements get copied over ?
this->adj = rhs.adj ; //more complicated vector, do elements get
copied over?
.....
}

Your elements will get copied but your copy ctor would be better like
this:
// Copy Constructor
MyClass(const MyClass& rhs) : arr_(rhs.arr_), adj(rhs.adj) {}
MyClass& operator=( const MyClass& rhs) {
if( this != rhs ) { // <- compiler barfs here
// how can I check to make sure !(same object)?
}

Your compiler barfs because you are comparing a pointer to a reference,
you should try
if (this != &rhs)
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top