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

Question on vector assignment and object equality determnation

P: n/a
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)?
}
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


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

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.