473,385 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jul 23 '05 #1
6 1381

"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?
Yes.
For example, if I wanted to read V2 data, could I use:
pRuttingData = new CRuttingDataV2;
Yes, as long as CRuttingDataV2 is derived from CRuttingData

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


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.
Jul 23 '05 #2

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:Xh******************@fe06.lga...

"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?
Yes.
For example, if I wanted to read V2 data, could I use:
pRuttingData = new CRuttingDataV2;


Yes, as long as CRuttingDataV2 is derived from CRuttingData


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.

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


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.

Jul 23 '05 #3
Jim Langston wrote:
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:Xh******************@fe06.lga...
"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

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

Yes, as long as CRuttingDataV2 is derived from CRuttingData


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


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
Jul 23 '05 #4
Jim Langston wrote:
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.


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

Josh

Jul 23 '05 #5
"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
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?
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.
For example, if I wanted to read V2 data, could I use:
pRuttingData = new CRuttingDataV2;


Yes.
--
John Carson

Jul 23 '05 #6

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

HTH,
--ag


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

ben
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
4
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not...
3
by: Mark A. Gibbs | last post by:
Good day, i'm having a bit of trouble with a base class i'm working on. this is what it boils down to: template <typename T> class foo { protected: foo() { T* p = static_cast<T*>(this); }
5
by: Tony Johansson | last post by:
Hello! If you have the following inheritance A is the base class for the derived class B and B is the base class for the derived class C. In this case will the constructor of class C be executed...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
52
by: junky_fellow | last post by:
char *str1 = "Hello"; char arr1 = { "Hello" }; char arr2 = { 'H', 'e', 'l', 'l', 'o' }; Is it legal to modify str1, arr1 and arr2 ?
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
6
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.