 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Brett Irving Guest
|
Posted: Sun Jun 29, 2003 9:12 am Post subject: please help!! assignment operator |
|
|
Hi could someone please help me on this cause I keep having problems
with it
I want to be able to assign one object to another but everytime I do
so it keeps segfaulting on me, Ive tried about 10 different ways of
trying to get this right and it just wont work
here is my code
I have globals char* name and int bikeReg
cyclist& cyclist::operator=(const cyclist& cyc)
{
if( this != &cyc )
{
delete[] name;
name = new char[strlen(cyc.name)+1]; // get new space
strcpy(name, cyc.name); // copy new name
bikeReg = cyc.bikeReg;
}
return *this;
}
Thanks a lot
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Sun Jun 29, 2003 10:30 am Post subject: Re: please help!! assignment operator |
|
|
"Brett Irving" <balgorg (AT) hotmail (DOT) com> wrote
| Quote: | Hi could someone please help me on this cause I keep having problems
with it
I want to be able to assign one object to another but everytime I do
so it keeps segfaulting on me, Ive tried about 10 different ways of
trying to get this right and it just wont work
here is my code
I have globals char* name and int bikeReg
|
Members, I suggest, not globals.
| Quote: |
cyclist& cyclist::operator=(const cyclist& cyc)
{
if( this != &cyc )
{
delete[] name;
|
I can see nothing wrong here. Is it possible that you have not initialized
'name' in your constructor? If you haven't, then deleting it is likely to
crash the program.
| Quote: | name = new char[strlen(cyc.name)+1]; // get new space
strcpy(name, cyc.name); // copy new name
bikeReg = cyc.bikeReg;
}
return *this;
}
|
David
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|