Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:21:22+00:00 2026-05-23T01:21:22+00:00

This question is very similar to this one Why can't I dynamic_cast "sideways" during

  • 0

This question is very similar to this one Why can't I dynamic_cast "sideways" during multiple inheritence?, except that the cast does work – just not inside in the constructor.

Header:

class A  
{  
public:  
    virtual                ~A() {}
    void                    printA();
};

class B
{
public:
                            B();
    virtual                ~B() {}
    void                    printB();

private:
    std::string             message_;
};

class C : public A, public B
{
public:
                        C() {}
    virtual                ~C() {}
};

Source:

void A::printA() { cout << "A" << endl; }
B::B()
{
    A* a = dynamic_cast< A* >( this );
    if ( a ) {
        message_ = std::string( "A and B" );
    } else {
        message_ = std::string( "B" );
    }
}
void B::printB() { cout << message_.c_str() << endl; }

Main:

int main( int argc, char* argv[] )
{
    cout << "Printing C..." << endl;
    C c;
    c.printA();
    c.printB();

    cout << "Checking again..." << endl;
    cout << !!dynamic_cast< A* >( &c ) << endl;

    return EXIT_SUCCESS;
}

Result:

Printing C...
A
B
Checking again...
1

So, the dynamic_cast does work for multiple inheritance (no surprises there!), but why not when called at runtime for the ‘this’ pointer inside B::B()? I thought that the object was fully formed once inside the body of the constructor i.e. all the memory was allocated for the component objects, they haven’t been initialised yet. I appreciate that this depends on the superclass constructor order, but in this example A is called before B.

I am obviously not understanding what exactly is happening under the hood, can someone please enlighten me?

Thanks,
Cam Bamber.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:21:22+00:00Added an answer on May 23, 2026 at 1:21 am

    Basically the standard says it will not work (dynamic_cast) during construction of an object.
    <quote>

    Edit: Added based on VJo comment below.

    Note: The cast from a ‘B’ to an ‘A’ using dynamic cast should work because we are casting an object of type ‘C’. If we added the following code to main:

    B  bObj;
    B& bRef = c;
    B* bPtr = &c;
    std::cout << !!dynamic_cast<A*>(&bObj) << std::endl;
    std::cout << !!dynamic_cast<A*>(&bRef) << std::endl;
    std::cout << !!dynamic_cast<A*>( bPtr) << std::endl;
    

    The extra output would be:

    0   // Can not convert a B to an A
    1   // Can convert this B to an A because it is really a C.
    1   // This is  what we are reeling doing in B::B() that fails
        // It is not the dynamic_cast<> that fails but the conversion of this from C* to B*
        // That is causing UB
    

    It fails in the constructor because the object is not fully formed. Using this we are trying to convert a C pointer into a B pointer before the C constructor has started (the code defined by the user). Thus the use of this in B::B() as a pointer to a C object fails thus when the dynamic_cast<> is called on this it fails to do what you want it to because of UB.

    12.7 Construction and destruction [class.cdtor]

    Paragraph 3

    To explicitly or implicitly convert a pointer (a glvalue) referring to an object of class X to a pointer (reference) to a direct or indirect base class B of X, the construction of X and the construction of all of its direct or indirect bases that directly or indirectly derive from B shall have started and the destruction of these classes shall not have completed, otherwise the conversion results in undefined behavior. To form a pointer to (or access the value of) a direct non-static member of an object obj, the construction of obj shall have started and its destruction shall not have completed, otherwise the computation of the pointer value (or accessing the member value) results in undefined behavior.

    [ Example:

    struct A { };
    struct B : virtual A { };
    struct C : B { };
    struct D : virtual A { D(A*); };
    struct X { X(A*); };
    struct E : C, D, X 
    { 
        E() : D(this),  // undefined: upcast from E* to A*
                        // might use path E* → D* → A* 
                        // but D is not constructed 
                        // D((C*)this), 
                        // defined: 
                        // E* → C* defined because E() has started 
                        // and C* → A* defined because
                        // C fully constructed 
          X(this) { // defined: upon construction of X,
                        // C/B/D/A sublattice is fully constructed
          } 
    };
    

    — end example ]

    </quote>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I realise that this is a very similar question to this one: Which
My question is very similar to this one , except I'm trying to capture
My question is very similar to this unanswered one, with some small differences that
Preamble: My core question is very similar to this one: How can I write
My question popped up a very similar question, this one . But the accepted
I know that this question is very similar to the question posted here .
This is very similar to another recent question: How can I return the current
I know that this is a repeated question. I have found very similar questions
I can see this question (or a similar one) has been asked a few
This question may be very similar to my one, but I cannot see the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.