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

  • Home
  • SEARCH
  • 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 7194387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:21:40+00:00 2026-05-28T20:21:40+00:00

I’m creating a couple of interfaces intended to provide access to a callback functionality.

  • 0

I’m creating a couple of interfaces intended to provide access to a callback functionality. That is, inheriting from interface A allows a class to use callbacks of type one; interface B allows type two. Inheriting from both A and B allows callbacks of both types. The ultimate purpose is that classes A and B will take care of all the dirty work by just inheriting from them.

First Problem

Here’s a small example that should illustrate some of the trouble I’m having:

class A
{
public:
    static void AFoo( void* inst )
    {
        ((A*)inst)->ABar( );
    }
    virtual void ABar( void ) = 0;
};

class B
{
public:
    static void BFoo( void* inst )
    {
        ((B*)inst)->BBar( );
    }
    virtual void BBar( void ) = 0;
};

class C : public A, public B
{
public:
    void ABar( void ){ cout << "A"; };
    void BBar( void ){ cout << "B"; };
};

And by making the calls

C* c_inst = new C( );
void (*AFoo) (void*) = C::AFoo;
void (*BFoo) (void*) = C::BFoo;
AFoo( (void*)c_inst );
BFoo( (void*)c_inst );

I expect I’ll get "AB" as output. Instead I get "AA". Reversing the order of derived classes (B before A), produces "BB". Why is this?

Second Problem

The actual interfaces I’m using are templated, so the code appears more like

template <class T> class A
{
public:
    static void AFoo( void* inst )
    {
        ((T*)inst)->ABar( );
    }
    virtual void ABar( void ) = 0;
};

template <class T> class B
{
public:
    static void BFoo( void* inst )
    {
        ((T*)inst)->BBar( );
    }
    virtual void BBar( void ) = 0;
};

class C : public A<C>, public B<C>
{
public:
    void ABar( void ){ cout << "A"; };
    void BBar( void ){ cout << "B"; };
};

The reason for this is so A and B can do all the work, but their implementations don’t need to have any knowledge of C.

Now, calling with

C* c_inst = new C( );
void (*AFoo) (void*) = C::AFoo;
void (*BFoo) (void*) = C::BFoo;
AFoo( (void*)c_inst );
BFoo( (void*)c_inst );

produces the correct output: "AB".

This small example works fine here, but it doesn’t always work correctly in practice. Very strange things start happening, similar to the weirdness in the first problem above. The main issue appears to be that both virtual functions (or the static functions, or something) don’t always make it into C.

For instance, I can successfully call C::AFoo(), but not always C::BFoo(). And this is sometimes dependent on the order in which I derive from A and B: class C: public A<C>, public B<C> may produce code where neither AFoo or BFoo work, while class C: public B<C>, public A<C> may produce code where one of them works, or maybe both.

Since the classes are templated, I can remove the virtual functions in A and B. Doing so produces working code, so long as ABar and BBar exist in C of course. That’s acceptable, but not desired; I’d rather know what the problem is.

What possible reasons are there that the above code could cause bizarre problems?

Why does the second example produce the correct output where though the first doesn’t?

  • 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-28T20:21:41+00:00Added an answer on May 28, 2026 at 8:21 pm

    You’re invoking undefined behavior. You can cast an X* to a void*, but once you’ve done that, the only thing it is safe to cast that void* is to an X* (this is not entirely true, I’m oversimplifying but for the sake of argument pretend it is).

    Now why is the code behaving like it is? One way to implement MI is something akin to:

     struct A
     {
        A_vtable* vtbl;
     };
    
     struct B
     {
        B_vtable* vtbl;
     };
    
     struct C
     {
        struct A;
        struct B;
     };
    

    In this example A is first, but the order will be determined by the compiler. When you cast to void, you get a pointer to the beginning of C. When you cast that void* back, you’ve lost the information you need to adjust the pointer appropriately if necessary. Since both A and B have a single virtual function with the same signature, you end up calling the impl. of whichever class happens to be first in the object layout.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.