Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > please help!! assignment operator

Reply
Thread Tools

please help!! assignment operator

 
 
Brett Irving
Guest
Posts: n/a
 
      06-29-2003
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:perator=(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
 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      06-29-2003
"Brett Irving" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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.

>
> cyclist& cyclist:perator=(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.

> name = new char[strlen(cyc.name)+1]; // get new space
>
> strcpy(name, cyc.name); // copy new name
>
> bikeReg = cyc.bikeReg;
>
> }
> return *this;
> }


David



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments