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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:24:44+00:00 2026-06-07T15:24:44+00:00

I am trying confirm the theory behind something that I have already got working

  • 0

I am trying confirm the theory behind something that I have already got working in practice. The full setup is somewhat contorted, as the functionality is split between different dlls, but I’ll try to describe the situation:

class __declspec( dllexport ) BaseClass
/** This class' definition is available to everything,
 *  via .h file, .dll and .lib. */
{
    protected:
        std::string name;

    public:
        std::string GetName();
        /** This is implemented in BaseClass, and just returns this->name. */
}

class DerivedClass: public BaseClass
/** This class is created within the executable, but is not 'visible' to other
 *  dlls - either through .h files, .lib, or anything else. */
{
    public:
        DerivedClass();
        /** This sets this->name based on its own propertied. */
}

This upcasting works, but it requires full access to the definition of DerivedClass:

void* pointer;
DerivedClass* derived_pointer = reinterpret_class<DerivedClass*>(pointer);
BaseClass* base_pointer = dynamic_cast<BaseClass*>(derived_pointer);
base_pointer->GetName();

However, the following does NOT work:

void* pointer;
BaseClass* base_pointer = reinterpret_class<BaseClass*>(pointer);
base_pointer->GetName();

In order to get round this problem, I’ve implemented an interface:

class __declspec( dllexport ) IBaseClass
/** Fully virtual 'interface' class, in same file as BaseClass. */
{
    public:
        virtual std::string GetName() = 0;
}

class __declspec( dllexport ) BaseClass: public IBaseClass
/** This class' definition is available to
 *  everything, via .h file, .dll and .lib. */
{
    protected:
        std::string name;

    public:
        std::string GetName();
        /** This is implemented in BaseClass, and just returns this->name. */
}

class DerivedClass: public BaseClass
/** This class is created within the executable, but is not 'visible'
 *  to other dlls - either through .h files, .lib, or anything else. */
{
    public:
        DerivedClass();
        /** This sets this->name based on its own propertied. */
}

And now the following code does work:

void* pointer;
IBaseClass* ibase_pointer = reinterpret_class<IBaseClass*>(pointer);
ibase_pointer->GetName();

I have some dim memory of somebody telling me that casting to a fully virtual class is a special case – but I can’t remember why, or find anything on the web about it.

Please help me – why does my code work?!

  • 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-06-07T15:24:47+00:00Added an answer on June 7, 2026 at 3:24 pm

    This is completely dependent on class layout, which is implementation-defined and cannot be relied on. Specifically for MSVC, a good intro to class layout is http://www.openrce.org/articles/full_view/23 and it’s worth knowing that you can ask for class layout with /d1reportSingleClassLayout flag.

    In your case, since the first BaseClass has no virtual members it will be placed inside DerivedClass at a non-specified location. I’d guess that DerivedClass has some virtual members as otherwise I’d expect BaseClass to be at the start of DerivedClass and the reinterpret_cast to work. If it has virtual members you’d have:

    +--------------------+
    |DerivedClass vtable |
    |BaseClass::name     |
    |DerivedClass members|
    

    By adding the interface IBaseClass nothing changes; DerivedClass is still laid out as:

    +--------------------+
    |DerivedClass vtable |
    |BaseClass::name     |
    |DerivedClass members|
    

    However the DerivedClass vtable starts with the IBaseClass vtable:

    +---------------------+
    |IBaseClass::GetName  |
    |DerivedClass virtuals|
    

    so calling through the DerivedClass vtable works.

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

Sidebar

Related Questions

I am just trying to confirm something with htmlspecialchars . I have just converted
I am new to mysqli, and trying to confirm that if I so something
Trying to make a custom :confirm message for a rails form that returns data
I have a User model. I am trying to update a confirm attribute from
This may seem obvious to most people, but I'm just trying to confirm that
im trying to make a popup that comes up to confirm a users action
I´m trying to do an replacement for the javascript confirm(). I have found the
I am trying to make it so that the user is prompted to confirm
I have been searching trying to confirm my reading of the XML spec. My
I have two password fields and am trying to confirm they are the same

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.