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 7168369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:46:00+00:00 2026-05-28T14:46:00+00:00

This is a puzzler I ran across this week. It’s partly due to the

  • 0

This is a puzzler I ran across this week. It’s partly due to the fact that I have just returned to C++ coding after coding Java for a while. Given the following code:

class Base {
};

class A : Base {
public:
    virtual void run() { cout << "This is A." << endl; }
};
class B : Base {
public:
   virtual void run() { cout << "This is B." << endl; }
};

class C : A, B {
public:
   void run() { cout << "This is C." << endl; }
};

int main(int argc, char* argv[])
{
   shared_ptr<A> ptrToA = shared_ptr<C>(new C());

   cout << "Pointer to A: " << ptrToA.get() << endl;
   cout << "Dynamic Cast A ptr to C: " << dynamic_pointer_cast<C>(ptrToA) << endl;

   ptrToA->run();

   assert(dynamic_pointer_cast<C>(ptrToA));
   cout << "Success!" << endl;
}

Why does it produce the following output?

Pointer to A: 0x1f29c010
Dynamic Cast A ptr to C: 0
Running...
This is C.
tester-cpp: tester.cpp:89: int main(int, char **): Assertion `dynamic_pointer_cast<C>(ptrToA)' failed.

Because “This is C” prints out, it’s obvious that the polymorphism is working, but it fails when dynamic casting a shared_ptr from the “A” base class to a “C”. I wasted hours of time on this subtle issue this week! Hopefully any answers will save future coders with a similar issue from wasting so much time (the bug was very subtle, especially after coding Java for a while).

Why? (I’ll give you a hint…this code was compiled with the Intel C++ compiler 12.1.0 on Linux. I tried it with another compiler and my code is fails to compile!)

  • 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-28T14:46:01+00:00Added an answer on May 28, 2026 at 2:46 pm

    The fact that it fails to compile on another compiler is a hint: It really should fail to compile. This is because C privately inherits from A and B, so C* should not be convertible to A*. Therefore shared_ptr<A> ptrToA = shared_ptr<C>(new C()); should fail to compile, since the conversation constructor should only participate in overload resolution when the pointer can be converted according to the standard. So this looks like a bug in the standard library used by Intel C++.

    Change Class C: A, B to Class C: public A, public B and it should work. Testing on gcc 4.6, the code indeed fails to compile with private inheritance and works just as it should with public inheritance of A.

    Since your code contains a diamond inheritance, you might also want to take a look at virtual inheritance.

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

Sidebar

Related Questions

Just ran into this recently and I thought it'd be helpful to share. The
Okay this is a bit of a noobish question, but I ran across this
After reading this article on Herb Sutter's blog, I experimented a bit and ran
I've just stumbled across this folder structure on my machine which contains lots of
Here's a puzzler. I have a variable that contains computer file paths. I need
I just ran into this line of code: if( lineDirection.length2() ){...} where length2 returns
I've got a little puzzler here for the bash scripting experts... I have a
Please help me on this puzzle i have class D inherited fronm class B
This question concerns running python files in terminal that are not stored in the
Originally, I was having some issues getting this code to function, but after a

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.