Assignment operator?

A

Aneel

Does assignment operator also creates object, as created by copy
constructor, in classes. e.g.

class check {
int x;
public:
check(int a=0):x(a){cout<<"\nconstructor\n";}

check operator = (const check &rhs){cout<<"\nassignment operator
\n";}
~check(){cout<<"\ndestructor\n";}
};

int main() {
check ob1(3), ob2(4);

ob1=ob2; // does this create a temporary copy of ob1 or ob2;

return 0;
}


return 0;
}
 
J

Jonathan Lee

Does assignment operator also creates object, as created by copy
constructor, in classes. e.g.

Not usually, but you've written operator=() to return by value.
Normally you'd have a return value in that function and it would
be

return *this;

With return by value, a copy will be made. To fix this, return
by reference. i.e.,

check& operator=(const check&) {
// ...
return *this;
}

--Jonathan
 
K

Kai-Uwe Bux

Aneel said:
Does assignment operator also creates object, as created by copy
constructor, in classes. e.g.

class check {
int x;
public:
check(int a=0):x(a){cout<<"\nconstructor\n";}

check operator = (const check &rhs){cout<<"\nassignment operator
\n";}
~check(){cout<<"\ndestructor\n";}
};

int main() {
check ob1(3), ob2(4);

ob1=ob2; // does this create a temporary copy of ob1 or ob2;

return 0;
}


return 0;
}

In the above case, it does not (*). But it could, e.g., in the copy-swap
idiom:

check & operator= ( check other ) {
swap( *this, other );
return ( *this );
}

(*) Well, according to the proposal for C++0X it does not. The current
standard [8.5.3/5] allows that during initialization of a const& a temporary
may be created. So calling the assignment operator could create a temporary,
but (a) it need not, (b) it's unlikely to, and (c) should not in the
(hopefully) near future.


Best

Kai-Uwe Bux
 
A

AnonMail2005

Does assignment operator also creates object, as created by copy
constructor, in classes. e.g.

class check {
    int x;
  public:
    check(int a=0):x(a){cout<<"\nconstructor\n";}

    check operator = (const check &rhs){cout<<"\nassignment operator
\n";}
    ~check(){cout<<"\ndestructor\n";}

};

int main() {
  check ob1(3), ob2(4);

  ob1=ob2; // does this create a temporary copy of ob1 or ob2;

  return 0;

}

  return 0;

}

In short, a copy constructor creates a new object in a new memory area
and initializes it using the object copied from. An assignment
operator "updates" an already existing object in memory using the
object assigned from.

HTH
 
N

Noah Roberts

Does assignment operator also creates object, as created by copy
constructor, in classes. e.g.

class check {
int x;
public:
check(int a=0):x(a){cout<<"\nconstructor\n";}

check operator = (const check &rhs){cout<<"\nassignment operator
\n";}
~check(){cout<<"\ndestructor\n";}
};

int main() {
check ob1(3), ob2(4);

ob1=ob2; // does this create a temporary copy of ob1 or ob2;

return 0;
}

Impossible to know, really. Your assignment operator claims to return
something but doesn't.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top