Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 06:49 AM
Josh Mcfarlane
Guest
 
Posts: n/a
Default Question about inheritance and pointer assignment

Ok, this may be a simple question.

I have a base class, CDatabase. This class contains pointers to other
classes containing data from raw files. Due to the way our system is
set up, the format of these raw files has changed so I need multiple
ways to read them. The old programmer had massive amounts of switch
statements depending on the version, I want to simplify this.

If I have the following in CDatabase:
CRuttingData * pRuttingData;

can I then assign pRuttingData to any inheritted members of
CRuttingData?

For example, if I wanted to read V2 data, could I use:
pRuttingData = new CRuttingDataV2;


If this method works, I can then only have to deal with data reading on
the inheritted class level and in the initialization of CDatabase vs in
every function.

Thanks,
Josh McFarlane

  #2  
Old July 23rd, 2005, 06:49 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: Question about inheritance and pointer assignment


"Josh Mcfarlane" <darsant@gmail.com> wrote in message
news:1120341854.863166.216020@f14g2000cwb.googlegr oups.com...[color=blue]
> Ok, this may be a simple question.
>
> I have a base class, CDatabase. This class contains pointers to other
> classes containing data from raw files. Due to the way our system is
> set up, the format of these raw files has changed so I need multiple
> ways to read them. The old programmer had massive amounts of switch
> statements depending on the version, I want to simplify this.
>
> If I have the following in CDatabase:
> CRuttingData * pRuttingData;
>
> can I then assign pRuttingData to any inheritted members of
> CRuttingData?[/color]

Yes.
[color=blue]
> For example, if I wanted to read V2 data, could I use:
> pRuttingData = new CRuttingDataV2;[/color]

Yes, as long as CRuttingDataV2 is derived from CRuttingData
[color=blue]
>
> If this method works, I can then only have to deal with data reading on
> the inheritted class level and in the initialization of CDatabase vs in
> every function.
>
> Thanks,
> Josh McFarlane[/color]

Make sure the methods that change in CRuttingDataV2 are defined
as virtual in CRunningData. If they are not defined as virtual in
CRunningData then when you use them you will use CRunningData's
methods and not CRunningDataV2's methods.

This is how polymorphism works.


  #3  
Old July 23rd, 2005, 06:49 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: Question about inheritance and pointer assignment


"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:XhExe.11435$Si3.2946@fe06.lga...[color=blue]
>
> "Josh Mcfarlane" <darsant@gmail.com> wrote in message
> news:1120341854.863166.216020@f14g2000cwb.googlegr oups.com...[color=green]
>> Ok, this may be a simple question.
>>
>> I have a base class, CDatabase. This class contains pointers to other
>> classes containing data from raw files. Due to the way our system is
>> set up, the format of these raw files has changed so I need multiple
>> ways to read them. The old programmer had massive amounts of switch
>> statements depending on the version, I want to simplify this.
>>
>> If I have the following in CDatabase:
>> CRuttingData * pRuttingData;
>>
>> can I then assign pRuttingData to any inheritted members of
>> CRuttingData?[/color]
>
> Yes.
>[color=green]
>> For example, if I wanted to read V2 data, could I use:
>> pRuttingData = new CRuttingDataV2;[/color]
>
> Yes, as long as CRuttingDataV2 is derived from CRuttingData[/color]

Actually, I think you have to do pRuttingData = (CRuttingData*) new
CRuttingDataV2.

Not positive if cast is neccessary or not. try it without, and if it
complains cast it.[color=blue]
>[color=green]
>>
>> If this method works, I can then only have to deal with data reading on
>> the inheritted class level and in the initialization of CDatabase vs in
>> every function.
>>
>> Thanks,
>> Josh McFarlane[/color]
>
> Make sure the methods that change in CRuttingDataV2 are defined
> as virtual in CRunningData. If they are not defined as virtual in
> CRunningData then when you use them you will use CRunningData's
> methods and not CRunningDataV2's methods.
>
> This is how polymorphism works.[/color]


  #4  
Old July 23rd, 2005, 06:49 AM
Artie Gold
Guest
 
Posts: n/a
Default Re: Question about inheritance and pointer assignment

Jim Langston wrote:[color=blue]
> "Jim Langston" <tazmaster@rocketmail.com> wrote in message
> news:XhExe.11435$Si3.2946@fe06.lga...[color=green]
>> "Josh Mcfarlane" <darsant@gmail.com> wrote in message
>> news:1120341854.863166.216020@f14g2000cwb.googlegr oups.com...[color=darkred]
>>> Ok, this may be a simple question.
>>>
>>> I have a base class, CDatabase. This class contains pointers to other
>>> classes containing data from raw files. Due to the way our system is
>>> set up, the format of these raw files has changed so I need multiple
>>> ways to read them. The old programmer had massive amounts of switch
>>> statements depending on the version, I want to simplify this.
>>>
>>> If I have the following in CDatabase:
>>> CRuttingData * pRuttingData;
>>>
>>> can I then assign pRuttingData to any inheritted members of
>>> CRuttingData?[/color]
>> Yes.
>>[color=darkred]
>>> For example, if I wanted to read V2 data, could I use:
>>> pRuttingData = new CRuttingDataV2;[/color]
>> Yes, as long as CRuttingDataV2 is derived from CRuttingData[/color]
>
> Actually, I think you have to do pRuttingData = (CRuttingData*) new
> CRuttingDataV2.[/color]

NO! Not only is it not necessary, it would be wrong.

HTH,
--ag

[The rest of your argument, however, is spot on. ;-)]
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
  #5  
Old July 23rd, 2005, 06:49 AM
Josh Mcfarlane
Guest
 
Posts: n/a
Default Re: Question about inheritance and pointer assignment

Jim Langston wrote:[color=blue]
> Make sure the methods that change in CRuttingDataV2 are defined
> as virtual in CRunningData. If they are not defined as virtual in
> CRunningData then when you use them you will use CRunningData's
> methods and not CRunningDataV2's methods.
>
> This is how polymorphism works.[/color]

Thanks, I had gotten that much into my head, I just wasn't sure about
assignment with the new operator.

Josh

  #6  
Old July 23rd, 2005, 06:49 AM
John Carson
Guest
 
Posts: n/a
Default Re: Question about inheritance and pointer assignment

"Josh Mcfarlane" <darsant@gmail.com> wrote in message
news:1120341854.863166.216020@f14g2000cwb.googlegr oups.com[color=blue]
> Ok, this may be a simple question.
>
> I have a base class, CDatabase. This class contains pointers to other
> classes containing data from raw files. Due to the way our system is
> set up, the format of these raw files has changed so I need multiple
> ways to read them. The old programmer had massive amounts of switch
> statements depending on the version, I want to simplify this.
>
> If I have the following in CDatabase:
> CRuttingData * pRuttingData;
>
> can I then assign pRuttingData to any inheritted members of
> CRuttingData?[/color]

At the risk of being accused of pedantry, the answer is no, but you are
actually doing the opposite in the line below, i.e., you are assigning a
pointer to a derived class object to pRuttingData. You are not assigning
pRuttingData to anything.

The rule is that pointers to a derived classe can be assigned to pointers to
its base class without a cast, and it is safe to do so. You can do the
reverse with a cast, but it is dangerous to do so. The first assignment is
safe because you can only call base class functions from a base class
pointer and the derived class is guaranteed to have them. The second
procedure is dangerous because you can call derived class functions from a
derived class pointer and the base class may not have them.
[color=blue]
> For example, if I wanted to read V2 data, could I use:
> pRuttingData = new CRuttingDataV2;[/color]

Yes.


--
John Carson

  #7  
Old July 23rd, 2005, 06:49 AM
benben
Guest
 
Posts: n/a
Default Re: Question about inheritance and pointer assignment

[color=blue]
>
> NO! Not only is it not necessary, it would be wrong.
>
> HTH,
> --ag
>[/color]

It is indeed unnecessary, but how could that be wrong?

ben


 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles