My memory fails me, inheritance and casting

This is a discussion on My memory fails me, inheritance and casting within the C++ Programming forums, part of the General Programming Boards category; I read about this somewhere, but I completely forget where, so maybe someone could help me. This problem deals with ...

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    My memory fails me, inheritance and casting

    I read about this somewhere, but I completely forget where, so maybe someone could help me. This problem deals with inheritance and casting. As an example, I'll use, class A and class B which is a subclass of A.
    Code:
    class A {
         int num;
    }
    
    class B : public A {
         int num2;
    }
    You can't cast A into B, because an A is not a B, but a B is an A, so you can cast it, however you will lose certain data. For example:
    Code:
    A anA;
    anA.num = 5;
    B aB;
    ab.num = 1;
    ab.num2 = 2;
    anA = (A)aB;
    That should work, but anA is not a B so it can not hold the num2 value, so data would be lost in the conversion. But I read a way to get around this, using pointers or something. Anyone have any idea?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I don't remember pointers fixing any such problem, but I do remember that you have to be careful with your pointers when you expect casting...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    What do you want to do with num2? Get rid of it, store it, maybe throw it in the ocean? Maybe I'm just stupid, but I don't quite understand what you want to do with it.

    This is just guess work, but maybe overloading the operator =, which would set anA.num to be aB.num(2?), is what you are looking for.
    To code is divine

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    A anA;
    anA.num = 5;
    B aB;
    ab.num = 1;
    ab.num2 = 2;
    anA = (A)aB;
    this code creates a new instance of a new A object.
    To keep a B object and change only a pointer yo have 2 choices.

    Code:
    A anA;
    anA.num = 5;
    B *aB = dynamic_cast<B*>(&anA);
    ab->num = 1;
    ab->num2 = 2;
    
    //or
    A anA;
    anA.num = 5;
    B &aB = dynamic_cast<B&>(anA);
    ab.num = 1;
    ab.num2 = 2;
    This is how you keep the original object that use inheritance to handle different pointers.
    To could also use static_cast, but it could cause a runtime-error if the class instance to be casted isn't the of the type of the cast. To prevent that kind of errors there is dynamic_cast which return NULL is the original object canęt be casted. Therefore you can only use dynamic_cast with pointers, not with objects os references.
    Plus, dynamic_cast checks the type of the object at runtime. To do so the dynamic_cast need info which will help in determinig is the class is valid. I don't kno what the standard says about this, but for MS compilers, each class much have at least a virtual method non-pure. The vtable will be used for comparison at runtime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. static casting problem
    By rxg00u in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2002, 08:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21