473,382 Members | 1,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Assignment operator

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!
Jul 22 '05 #1
4 1367
"al" <al***@168.net> wrote in message
news:hX***********************@bgtnsc04-news.ops.worldnet.att.net...
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! :)

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

"al" <al***@168.net> wrote in message
news:hX***********************@bgtnsc04-news.ops.worldnet.att.net...
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::operator= 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 <Base *> mySmartList;

Thank you very much!


HTH
Dan
Jul 22 '05 #3
I read Cy's answer and I realised that I gave one answer for two questions.
See below

"Dan Cernat" <ce****@dan.com> wrote in message
news:vu************@corp.supernews.com...

"al" <al***@168.net> wrote in message
news:hX***********************@bgtnsc04-news.ops.worldnet.att.net...
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?
Nope, it is the copy constructor called (as Cy said)
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::operator= 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::operator= 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

Thank you very much!


HTH
Dan


Dan
Jul 22 '05 #4
al wrote:
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); }

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
9
by: Matthew Polder | last post by:
Hi, When a class Apple is written and the assignment operator is not explicitly declared, the operator Apple& operator=(const Apple&) is created by the compiler. Is there any difference...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.