473,811 Members | 3,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment operator

al
string s = "original string";

s = "new string";
s.assign("anoth er 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_bac k(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 1393
"al" <al***@168.ne t> wrote in message
news:hX******** *************** @bgtnsc04-news.ops.worldn et.att.net...
string s = "original string";

s = "new string";
s.assign("anoth er 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_bac k(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.ne t> wrote in message
news:hX******** *************** @bgtnsc04-news.ops.worldn et.att.net...
string s = "original string";

s = "new string";
s.assign("anoth er 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_bac k(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.super news.com...

"al" <al***@168.ne t> wrote in message
news:hX******** *************** @bgtnsc04-news.ops.worldn et.att.net...
string s = "original string";

s = "new string";
s.assign("anoth er 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_bac k(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("anoth er 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
17881
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
5
4838
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 to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
16
2624
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
9
3069
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, Gary Moore LP, LP Faded DC, Jeff Beck Strat, Morgan OM Acoustic,
9
1713
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 between this and const Apple& operator=(const Apple&)
1
1818
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& operator(int i); }
6
13388
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
2408
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
2295
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
9
3829
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 object? reference or const reference? deep copy or shallow copy is used in default assignment operator?)? I have the C++ Programming Book at hand, but can not find it from Index page.
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.