C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

please help!! assignment operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Brett Irving
Guest





PostPosted: Sun Jun 29, 2003 9:12 am    Post subject: please help!! assignment operator Reply with 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


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





PostPosted: Sun Jun 29, 2003 10:30 am    Post subject: Re: please help!! assignment operator Reply with quote



"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.