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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:35:01+00:00 2026-06-05T12:35:01+00:00

Suppose I have a pair of base classes: class A {}; class B {

  • 0

Suppose I have a pair of base classes:

class A
{};

class B
{
    virtual void foo(A* ref);
    virtual void foo2(A* ref);
};

And from them, a few derived classes:

class C : virtual public A
{
   int data;
};

class D : public B
{
    virtual void foo(A* ref)
    {
        ((C*) (ref)).data = 5;
    }
};

class E : virtual public A
{
    int otherData;
};

class F : public B
{
    virtual void foo2(A* ref)
    {
        ((E*) (ref)).otherData = 6;
    }
};

And finally, we have a class that follows suit, as such:

class G : public E, public C
{
};

with a main function that goes as follows:

int main()
{
   B* myD = new D();
   B* myF = new F();

   A* myA = new G();

   myD->foo(myA);
   myF->foo2(myA);

   return 0;
}

Ok, IGNORING the obvious fact that this is a ‘dreaded diamond’ (which, the “multiple As” problem has been averted due to virtual), the general idea is that I want to have all my objects be Gs, Ds and Fs, but stored as references to As and Bs. I want to be able to use B’s (which is actually always either D or F) virtual functions to modify the values of G… However, A and B do not know of any of their child classes, and thus cannot be declared using them. This means that the virtual function arguments must always be a pointer to an A. However, D wants to have it’s input always be a C, and F wants it’s input to be an E. While this might not have been an issue if G was inheriting from only one parent, our current G is inheriting from 2 parents; I do not know how the this pointers actually work in the underlying context of situations like this… however, I do not feel like this is going to be a possible situation.

Would anyone be willing to shed some light on the mechanisms of casting pointers to parent/child classes, and if there would be any way this could be pulled off?

  • 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-05T12:35:03+00:00Added an answer on June 5, 2026 at 12:35 pm

    Assuming you have RTTI switched on, which you most likely do, you can use dynamic_cast to attempt to up-cast a base class pointer to a derived (or sibling) class pointer. The referenced object is tested at runtime to see if it is a compatible class, if this is not a valid upcast than it will return 0, in which case you can test for that and branch.

    http://en.wikipedia.org/wiki/Dynamic_cast

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

Sidebar

Related Questions

suppose we have these classes: public class Record { int key; int value; Record(){
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have string Name and Image Photo as properties of a class in
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
Suppose I have a PL/SQL stored procedure as follows: PROCEDURE do_something(foo VARCHAR2 DEFAULT NULL)
Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots;
suppose you have a HashMap m and there is already a key value pair
Suppose I have a string that looks like the following: string foo =<add key=key1
Suppose we have a function like this one: foo (x, _, y) = (x,
suppose I have a std::vector of pair. How can I use, efficiently, method std::find

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.