| | |
copy constructor problem
![]() |
•
•
Join Date: Oct 2003
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2003
Posts: 22
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Oct 2003
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
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*'
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*'
•
•
•
•
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
![]() |
Similar Threads
- Help with getting multiple copies(copy constructor help) (C++)
- copy constructor question (C++)
- help! how to implement a copy constructor in c++ for ADT queues?? (C++)
- (C++) Writing a Copy Constructor?!? (C++)
Other Threads in the C++ Forum
- Previous Thread: vtable
- Next Thread: Is this a runtime error?
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output pointer problem program programming project python random read recursion reference rpg shutdown() std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets





