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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:17:28+00:00 2026-05-26T23:17:28+00:00

In a project of mine here I have a class that implements four interfaces:

  • 0

In a project of mine here I have a class that implements four interfaces:

class A : public B,
          public C,
          public D,
          public E
{
  ----Implementation Code here----
};

these four interfaces contains only pure virtual functions, none of them compose the diamond problem (so I don’t need to use the virtual keyword) so I spected no problem when doing something like this:

A* var = new A;
((C*)var)->method_from_interface();

Yet something ugly is happening, because the function is jumping around to a different method of the A class, and valgrind complains about an unhandled instruction. However, doing this:

 A* var = new A;
(dynamic_cast<C*>(var))->method_from_interface();

works as suspected.

So I’m wondering if this is a G++ bug or some misuse of the language?

edit:

Maybe I’ve simplified too much my problem. I’m receiving the A class as a D* on a function call:

void do_something(provider* p) {
   D* iface = p->recoverItemByName("nameThatReturnsClassA");
   ((B*) iface)->call_method_from_b_iface();
}

Note that I know that in this time the D* is in fact an A*, so casting doing a casting to B* isn’t breaking any rules. I can’t static_cast it, though, as D* and B* have no relation, but I can use reinterpret_cast successfully.

  • 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-26T23:17:29+00:00Added an answer on May 26, 2026 at 11:17 pm

    Note that I know that in this time the D* is in fact an A*, so casting
    doing a casting to B* isn’t breaking any rules.

    Wrong.

    You may know that it’s actually an A*, but the compiler doesn’t, at least at compile time when it’s trying to figure out what code to emit to do the conversion.

    A funny thing happens when you inherit from multiple classes with virtual methods. When you convert from the derived class to one of the interface classes, the pointer address changes! The compiler does adjustments to the pointer to make it valid for the pointer type you’ve declared. Try it yourself, start with an A* pointer and display its value, then cast to a B*, C*, and D* and display those. At most one of the base classes will be the same as the A*, the others will be different.

    When you use a C-style cast you’re telling the compiler “I don’t care if you can’t do the conversion properly, do it anyway.” It duly treats the D* as a B* without doing the required fixups, so now the pointer is completely wrong. It isn’t pointing to the class B vtable so the wrong methods get called.

    A dynamic_cast works because it uses extra information available at run-time; it can trace the pointer to its most-derived A* and then back down to a B*.

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

Sidebar

Related Questions

Developing a project of mine I realize I have a need for some level
For a C# project of mine the code metrics delta after a refactoring are:
I have an issue with a project of mine. I have a Menu UIViewController
I'm working on this project of mine that requires to define lat/lng elements by
I have a website project that is consuming user controls from another shared project.
I have been trying to get a project of mine to run but I
I have to do a class project for data mining subject. My topic will
I have been playing around with ElasticSearch for a new project of mine. I
I'm working on a small hobby project of mine where I have a big
I have a question, I'm currently working on a little project of mine and

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.