Question about inheritance and pointer assignment

Discussion in 'C++' started by Josh Mcfarlane, Jul 2, 2005.

  1. 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
    Josh Mcfarlane, Jul 2, 2005
    #1
    1. Advertising

  2. Josh Mcfarlane

    Jim Langston Guest

    "Josh Mcfarlane" <> wrote in message
    news:...
    > 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.
    Jim Langston, Jul 2, 2005
    #2
    1. Advertising

  3. Josh Mcfarlane

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:XhExe.11435$...
    >
    > "Josh Mcfarlane" <> wrote in message
    > news:...
    >> 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.
    Jim Langston, Jul 2, 2005
    #3
  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
    Josh Mcfarlane, Jul 2, 2005
    #4
  5. Josh Mcfarlane

    Artie Gold Guest

    Jim Langston wrote:
    > "Jim Langston" <> wrote in message
    > news:XhExe.11435$...
    >> "Josh Mcfarlane" <> wrote in message
    >> news:...
    >>> 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
    Artie Gold, Jul 2, 2005
    #5
  6. Josh Mcfarlane

    John Carson Guest

    "Josh Mcfarlane" <> wrote in message
    news:
    > 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
    John Carson, Jul 3, 2005
    #6
  7. Josh Mcfarlane

    benben Guest


    >
    > NO! Not only is it not necessary, it would be wrong.
    >
    > HTH,
    > --ag
    >


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

    ben
    benben, Jul 4, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vasileios Zografos

    inheritance and assignment

    Vasileios Zografos, Jul 1, 2003, in forum: C++
    Replies:
    3
    Views:
    325
    Victor Bazarov
    Jul 2, 2003
  2. nagy
    Replies:
    36
    Views:
    981
    Terry Reedy
    Jul 20, 2006
  3. Replies:
    10
    Views:
    672
    Chris Torek
    Feb 4, 2005
  4. Replies:
    2
    Views:
    310
    Karl Heinz Buchegger
    Oct 3, 2005
  5. Chris
    Replies:
    34
    Views:
    1,487
Loading...

Share This Page