943,788 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8361
  • C++ RSS
Oct 13th, 2003
1

copy constructor problem

Expand Post »
Hi, i need help creating a copy constructor.
i have a class called CElist (and another called CEmployee)
and it contains methods like this one:

bool CElist::getFirst(CEmployee& e)
{
//do some stuff
//sets e to the first employee and returns true
//returns false if it cant
}

my problem is with the copy constructor:

CElist::CElist(const CElist& eList)
{
CEmployee newEmp;
eList.getFirst(newEmp); //this is the error part! --> cannot convert 'this' pointer from 'const class CElist' to 'class CElist &'

this.setFirst(newEmp);
//do other stuff
}

i dont think im changing the eList right? Right now the only solution i can think of is to create a different function that returns the first CEmployee, not a bool...
>< thanks for any help
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
bluegirl is offline Offline
3 posts
since Oct 2003
Oct 14th, 2003
0

Re: copy constructor problem

hmm.. well it works if i dont make the paramter const, so then i guess my question would be, does the parameter for a copy constructor always have to be const? (i know if im just copying values it should be, but....)

sorry, im new at c++ ><
Reputation Points: 11
Solved Threads: 0
Newbie Poster
bluegirl is offline Offline
3 posts
since Oct 2003
Oct 14th, 2003
1

Re: copy constructor problem

You only make something const(constant) if it's , well, constant. If you're going to change anything about it, or any sorts of funky errors pop up, you're better off not const-ing it.

I personally never use const, though I did take C first, so that might have something to do with it. If it's not absolutely required that you put const there, then don't.

What are you doing in your program, though?
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003
Oct 15th, 2003
0

Re: copy constructor problem

oh its to maintain a list of employees (for one of my assignments...) but its alrite^^ i fixed it --> didnt know that i can access the private data members of the passed instance in the constructor... i htought it would be illegal but guess not^__^
i learned java before, but now im doing c++, and its ...werid hehe
thanks for the reply.
Last edited by bluegirl; Oct 15th, 2003 at 4:36 am.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
bluegirl is offline Offline
3 posts
since Oct 2003
Oct 18th, 2003
1

Re: copy constructor problem

i thought it's wrong ,whick use const parameter to construct Class CELIST,illegel grammar.
CElist::CElist(const CElist& eList)//org
CElist::CElist(CElist& eList)//try it
Reputation Points: 11
Solved Threads: 0
Newbie Poster
robysad is offline Offline
1 posts
since Oct 2003
Jul 7th, 2005
0

Re: copy constructor problem

For me, the problem is the following :

in your CElist constructor, the parameter is const, so you cannot call methods that are not const on this object.

You have to change the signature of your getFirst method :

bool CElist::getFirst(CEmployee& e) const;

if this method do not modifies modifies the insternal state of the object (because now 'this' is a 'const CElist*'

Quote originally posted by bluegirl ...
Hi, i need help creating a copy constructor.
i have a class called CElist (and another called CEmployee)
and it contains methods like this one:

bool CElist::getFirst(CEmployee& e)
{
//do some stuff
//sets e to the first employee and returns true
//returns false if it cant
}

my problem is with the copy constructor:

CElist::CElist(const CElist& eList)
{
CEmployee newEmp;
eList.getFirst(newEmp); //this is the error part! --> cannot convert 'this' pointer from 'const class CElist' to 'class CElist &'

this.setFirst(newEmp);
//do other stuff
}

i dont think im changing the eList right? Right now the only solution i can think of is to create a different function that returns the first CEmployee, not a bool...
>< thanks for any help
Reputation Points: 11
Solved Threads: 6
Junior Poster
CrazyDieter is offline Offline
106 posts
since Jul 2005
Jul 7th, 2005
0

Re: copy constructor problem

Don't resurrect ancient threads.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: vtable
Next Thread in C++ Forum Timeline: Is this a runtime error?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC