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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:05:15+00:00 2026-05-21T09:05:15+00:00

Given the following class structure: class Base { virtual void outputMessage() { cout <<

  • 0

Given the following class structure:

class Base
{
    virtual void outputMessage() { cout << "Base message!"; }
};

class Derived : public Base
{
    virtual void outputMessage() { cout << "Derived message!"; }
}

.. and this code snippet:

Base baseObj;
Derived* convertedObj = (Derived*) &baseObj;
convertedObj->outputMessage();

.. the output will be “Base message!”.

Is there any way to cast or manipulate the object to make Derived’s version of the outputMessage method to be called polymorphically?

Edit: I will attempt to show the reason why I’m after this:

I am writing migration tools that hook into our main system. For this reason, I need to get access to protected member methods, or customise existing virtual methods. The former I can do by defining a derived class and casting objects to it, to call methods statically. What I can’t do is change the behaviour for methods which I do not call statically (ie methods that are called elsewhere in the codebase).

I have also tried creating objects of the derived class directly, but this causes issues in other parts of the system due to the manipulation of the objects passed through the constructor.

  • 1 1 Answer
  • 1 View
  • 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-21T09:05:15+00:00Added an answer on May 21, 2026 at 9:05 am

    No Standard-compliant solution

    What you’re trying to do isn’t possible using behaviours guaranteed by the C++ Standard.

    If you really MUST do this as a short-term measure to assist your migration, don’t depend on it in production, and can adequately verify the behaviour, you could experiment as illustrated below.

    Discussion of your attempt

    What I’m showing is that you’re taking the wrong approach: simply casting a pointer-to-base to a pointer-to-derived doesn’t alter the object’s vtable pointer.

    Deriving a plausible hack

    Addressing that, the naive approach is to reconstruct the object in place as a derived object (“placement” new), but this doesn’t work either – it will reinitialise the base class members.

    What you can possibly do is create a non-derived object that has no data members but the same virtual dispatch table entries (i.e. same virtual functions, same accessibility private/protected/public, same order).

    More warnings and caveats

    It may work (as it does on my Linux box), but use it at your own risk (I suggest not on production systems).

    Further warning: this can only intercept virtual dispatch, and virtual functions can sometimes be dispatched statically when the compiler knows the types at compile time.

    ~/dev cat hack_vtable.cc
    // change vtable of existing object to intercept virtual dispatch...
    
    #include <iostream>
    
    struct B
    {
        virtual void f() { std::cout << "B::f()\n"; }
    
        std::string s_;
    };
    
    struct D : B
    {
        virtual void f() { std::cout << "D::f()\n"; }
    };
    
    struct E
    {
        virtual void f() { std::cout << "E::f()\n"; }
    };
    
    int main()
    {
        B* p = new B();
        p->s_ = "hello";
        new (p) D();  // WARNING: reconstructs B members
    
        p->f();
        std::cout << '\'' << p->s_ << "'\n"; // no longer "hello"
    
        p->s_ = "world";
        new (p) E();
        p->f();  // correctly calls E::f()
        std::cout << '\'' << p->s_ << "'\n"; // still "world"
    }
    
    ~/dev try hack_vtable   
    make: `hack_vtable' is up to date.
    D::f()
    ''
    E::f()
    'world'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following structure... class Foo { public string Category { get; set; }
Given the following class structure, will Bar serialize/deserialize as expected? public class Foo {
Given the following structure: Public Class Vendor Public Property Accounts As Account() End Class
Given the following code: public class Product { public Guid Id {get;set;} } public
Given the following class: class A { public List<B> ListB; // etc... } where
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following code: final class retVal { int photo_id; } Gson gson =
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { ... } public class
Given the following object: public class Product { string Name {get;} int Quantity {get;}

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.