473,406 Members | 2,217 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,406 software developers and data experts.

assignment operator

254 100+
Expand|Select|Wrap|Line Numbers
  1. class menuitem {
  2. public:
  3.      menuitem(string s) :  text(s) {}
  4.      virtual ~menuitem() {}
  5.      virtual void display() { cout << text << endl; }
  6. private:
  7.      string text;
  8. };
  9.  
I was required to write additional methods for this class:
an copy constructor
an assignment operator

So this is what i have done:
for copy constructor:
Expand|Select|Wrap|Line Numbers
  1. menuitem(const menuitem& m) {
  2.       text = m.text ;
  3. }
  4.  
am i correct for the copy constructor?

the assignment operator, i'm not sure how to do it. any one could show it ?

thanks in advance.
fom
Nick

i'm not s
Feb 9 '07 #1
2 999
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. class menuitem {
  2. public:
  3.      menuitem(string s) :  text(s) {}
  4.      virtual ~menuitem() {}
  5.      virtual void display() { cout << text << endl; }
  6. private:
  7.      string text;
  8. };
  9.  
I was required to write additional methods for this class:
an copy constructor
an assignment operator

So this is what i have done:
for copy constructor:
Expand|Select|Wrap|Line Numbers
  1. menuitem(const menuitem& m) {
  2.       text = m.text ;
  3. }
  4.  
am i correct for the copy constructor?

the assignment operator, i'm not sure how to do it. any one could show it ?

thanks in advance.
fom
Nick

i'm not s
Nick-

I found this explanation of overloading the copy constructor, let me know what you think.

As for the copy constructor, the purpose of it is to return a completely separate object, with the same data. I know Java especially enforces the 'new' keyword, and I'm not sure by spot-reading your function if it will work as a copy or not - a way to check would be to have it like you do, and then change something in the new one, see if it changed the old.
Feb 13 '07 #2
Ganon11
3,652 Expert 2GB
Your copy constructor looks fine - it should work with no problems.

In your assignment operator, you will be setting the text fields equal to each other - just the same as your copy constructor.

The main use of the assignment operator and deconstructor is for preventing shallow copies of pointers - that is, two pointers pointing to the same data. Since you are implementing a string and not a char*, I'd have to say I don't find the addition of these operators necessary - unless, of course, this is a class assignment for practice, in which case you're stuck :).
Feb 13 '07 #3

Sign in to post your reply or Sign up for a free account.

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
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.