CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    1

    Struct inheritance

    Alright, simple problem, hopefully simple solution.
    This is going to seem like a stupid question, I know but I'm having some difficulty with struct inheritance.

    Here's what I have:

    struct A
    {
    //elements here
    }

    struct B:A
    {
    //more elements here

    }

    int main(void)
    {
    A* pa = new B();
    B* pb;
    pb = pa;//this is the important part

    return 0;
    }

    Do I need an explicit cast here?

    Also, say I was trying to do:
    A a;
    B b;
    b = a;

    Do I need a new constructor B(A a) in B to handle this or is there another way?

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Struct inheritance

    Code:
    A* pa = new B();
    B* pb;
    pb = pa;//this is the important part
    No, pb can't be assigned to pa in this fashion without a cast, because it can't be guaranteed that an object pointed to AS an a * is in fact (also) a b * (there could be other objects derived from A, which have nothing to do with B).

    It's not exactly safe to cast statically either, though you can do it if the object has no virtual functions. The problem is, even if you perform a static cast, the compiler can't always tell if doing that in that context if valid. That's called downcasting typically.

    What IS safe, but slow, is a dynamic cast.

    pb = dynamic_cast<B *>( pa );

    That's safe, but can only be performed in A has a virtual function (even a destructor).

    In fact, a dynamic_cast is a technique to determine IF pa is in fact a B. The result of the dynamic_cast is a runtime test, and if pa were not actually a B, then pb would be null.

    Also, pa = pb;

    That can be done anytime, because it is always true that B is an A.

    It is important, also, to understand that downcasting isn't safe using C style casts, as in....

    pb = (B *)pa;

    This kind of cast may work in some code - but it's not reliable. The reason is that in some layouts and compilers it may be that when a valid cast of type A to type B is performed, some pointer adjustment may be required. This C style (and static_cast) casts can't do that adjustment. Only dynamic_cast can.

    You should hope for designs that don't require casts, but if they do - cast upwards - toward a base. That's safe. Rely on virtual functions to "descend" downwards, whenever it's possible (and it usually is in reality).

    Code:
    Do I need a new constructor B(A a) in B to handle this or is there another way?
    Think about it a moment. In some object designs how would you construct a B in terms of an A. What if the a is really a C, derived from A. What would should a construct mean?

    You can do it, certainly. An automatic copy for statements b = a can't be created by the compiler without your human insight. If it did, how would it deal with the parts of B that A knows nothing about?
    Last edited by JVene; May 21st, 2009 at 07:02 PM.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: Struct inheritance

    That's safe, but can only be performed in A has a virtual function (even a destructor).
    It should probably be pointed out that any class that's going to be used polymorphically needs to have a virtual destructor anyway. Even if you don't actually need to do anything in the dtor, you need to declare it virtual in the base class(es).

    See http://www.parashift.com/c++-faq-lit....html#faq-20.7
    Last edited by Speedo; May 21st, 2009 at 11:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured