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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:02:42+00:00 2026-06-06T19:02:42+00:00

I have a class Derived that inherits directly from two base classes, Base1 and

  • 0

I have a class Derived that inherits directly from two base classes, Base1 and Base2. I’d like to know if it’s safe, in general, to compare pointers to the base classes to determine if they are the same Derived object:

Base1* p1;
Base2* p2;

/*
 * Stuff happens here. p1 and p2 now point to valid objects of either their
 * base type or Derived
 */

//assert(p1 == p2); //This is illegal
assert(p1 == static_cast<Base1*>(p2)); //Is this ok?
assert(static_cast<Derived*>(p1) == static_cast<Derived*>(p2)); //How about this?

The pointers are guaranteed to be valid, but not necessarily to point to a Derived object. My guess is that this is probably fine, but I wanted to know if it was ok from a technical C++ perspective. I actually never do any operations on the pointers, I just want to know if they point to the same object.

EDIT: It seems to be safe if I can guarantee that p1 and p2 point to Derrived objects. I basically want to know if it is safe if they don’t- if one or both point to a base object, will the comparison necessarily fail? Again, I can guarantee the pointers are valid (i.e., p1 would never point at a Base2 object or vice versa)

  • 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-06T19:02:47+00:00Added an answer on June 6, 2026 at 7:02 pm

    Well, no, it won’t work.

    I’m personally a big fan of learning-by-example, so here’s one:

    #include <iostream>
    
    class Base1
    {
    public:
        Base1()
        {
            numberBase1 = 1;
        }
    
        int numberBase1;
    };
    
    class Base2
    {
    public:
        Base2()
        {
            numberBase2 = 2;
        }
    
        int numberBase2;
    };
    
    class Derived : public Base1, public Base2
    {
    public:
        Derived()
        {
            numberDerived = 3;
        }
    
        int numberDerived;
    };
    
    int main()
    {
        Derived d;
        Base1 *b1 = &d;
        Base2 *b2 = &d;
    
        std::cout << "d: " << &d << ", b1: " << b1 << ", b2: " << b2 << ", d.numberDerived: " << &(d.numberDerived) << std::endl;
    
        return 0;
    }
    

    One run-through on my computer outputted this:

    d: 0035F9FC, b1: 0035F9FC, b2: 0035FA00, d.numberDerived: 0035FA04
    

    Soo.. If we define the address of d as 0, then b1 is 0, b2 is +4 and the number of d is +8. This is because an int on my machine is 4 byte long.

    Basically, you have to look at the layout of how C++ internally represents a class:

    Address:    Class:
    0           Base1
    4           Base2
    8           Derived
    

    .. So in total, instantiating a Derived class will allocate space for the base classes of the derived class, and finally make room for the derived object itself. Since we have 3 integers here, that’ll be 12 bytes.

    Now, what you’re asking (unless I misunderstood something) is if you can compare the address of the different base class pointers to each other to see if they point to the same object, and the answer is no – Not directly at least, as in my example, b1 would point to 0035F9FC, while b2 would point to 0035FA00. In C++, this offsetting is all done at compile time.

    You could probably do some magic with RIIA and sizeof() and determine how much of an offset b2 should have to be comparable to b1, but then you run into all kinds of other trouble like virtuals. In short, I would not recommend this approach.

    A much better way would be to cast to Derived* like ialiashkevich said, however, that would impose a problem if your object was not an instance of Derived*.

    (Disclaimer; I haven’t used C++ in 3-4 years, so I might be a bit off my game. Be gentle 🙂 )

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

Sidebar

Related Questions

I have a base class that inherits List<> which has multiple classes derived from
I have two objects that are derived from same the base class. Lets say
If you have a void* pointer to Derived class that inherits from both BaseA
I have created a BasePage class that inherits from System.Web.Ui.Page. In that base class
I have a two windows forms classes, a base class and a derived class.
I have a basic class that derived subclasses inherit from, it carries the basic
I have a c++ class derived from a base class in a framework. The
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
I have class Base and classes Derived_1 , Derived_2 ... I need derived classes
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC

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.