Assignment operator

A

al

string s = "original string";

s = "new string";
s.assign("another new string");

Is there any difference using above two way to assign a new string to s?


class Derived : public Base
{
};

Base b;
Derived d;
list<Base> mylist;
mylist.push_back(d);

Is assignment operator called? If true, whose operator is called, Base's or
Derived's?

b = d;

Whose assignment operator called, Base's or Derived's?

Thank you very much!
 
C

Cy Edmunds

al said:
string s = "original string";

s = "new string";
s.assign("another new string");

Is there any difference using above two way to assign a new string to s?
No.



class Derived : public Base
{
};

Base b;
Derived d;
list<Base> mylist;
mylist.push_back(d);

Is assignment operator called?

No, d should be copied into a new node using Base's copy constructor.
If true, whose operator is called, Base's or
Derived's?

b = d;

Whose assignment operator called, Base's or Derived's?
Base's.


Thank you very much!

You're welcome. Hope I got everything right! :)
 
D

Dan Cernat

al said:
string s = "original string";

s = "new string";
s.assign("another new string");

Is there any difference using above two way to assign a new string to s?

the result is the same: s gets the new value. how it is done is
implementation dependant.
class Derived : public Base
{
};

Base b;
Derived d;
list<Base> mylist;
mylist.push_back(d);

Is assignment operator called? If true, whose operator is called, Base's or
Derived's?

b = d;

Whose assignment operator called, Base's or Derived's?

yes, the assignment operator is called. Since you store Base objects in your
list, the d object will be trimmed down into a Base object and the
Base::eek:perator= will be called. thje list will store only teh Base portion
of the d object.

to store various objects in a container, use pointers to their base (they
have to have the same base)

list said:
Thank you very much!

HTH
Dan
 
D

Dan Cernat

I read Cy's answer and I realised that I gave one answer for two questions.
See below

Dan Cernat said:
the result is the same: s gets the new value. how it is done is
implementation dependant.
Base's

yes, the assignment operator is called. Since you store Base objects in your
list, the d object will be trimmed down into a Base object and the
Base::eek:perator= will be called. thje list will store only teh Base portion
of the d object.
The paragraph above should be (disregard the mumbling about list):
yes the assignment operator is called. the d object will be trimmed down
into a Base object and the Base::eek:perator= will be called.
to store various objects in a container, use pointers to their base (they
have to have the same base)

list <Base *> mySmartList;

this is correct

Dan
 
J

Jeff Schwab

al said:
string s = "original string";

s = "new string";
s.assign("another new string");

Is there any difference using above two way to assign a new string to s?

Grep your header. Here's what mine says:

basic_string&
operator=(const basic_string& __str) { return this->assign(__str); }
 

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

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top