CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2010
    Posts
    121

    Promoting a derived struct to another deriving struct

    Code:
    struct A
    {
    };
    
    struct B : public A
    {
    };
    
    A* a = new A();
    B* b = (B*) a;
    When will this downcast fail? I just got a bad pointer out of this cast...(0xbaadf00dbaadf00d )
    Thanks
    Jack
    Last edited by luckiejacky; November 17th, 2017 at 10:00 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Promoting a derived struct to another deriving struct

    Code:
    struct A
    {
       int a;
    };
    
    struct B : public A
    {
       double b;
    };
    
    A* a = new A();
    The created *a has no place for double b; that is appeared to belong in struct B.
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2010
    Posts
    121

    Re: Promoting a derived struct to another deriving struct

    Why this thing is always a problem? I mean I create FRAME's inside this function (the B) and return to the caller, then the caller accepts it as a derived class (the A)
    Code:
    newFrame->pFrameSibling 
    	= (FRAME*)CopyFrameHierarchy((const FRAME*)other->pFrameSibling);
    Even on an explict cast
    Last edited by luckiejacky; November 17th, 2017 at 07:34 PM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Promoting a derived struct to another deriving struct

    Quote Originally Posted by luckiejacky View Post
    Why this thing is always a problem? I mean I create FRAME's inside this function (the B) and return to the caller, then the caller accepts it as a derived class (the A)
    But in your OP the B is a derived class while A is the base one!
    Victor Nijegorodov

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