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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:08:39+00:00 2026-05-18T09:08:39+00:00

I was writing an operator== between two kinds of smart pointer and thought I

  • 0

I was writing an operator== between two kinds of smart pointer and thought I should run a quick sanity check. I’m suprised by the result…

In the snippet below how is it that all variants of f and b end up with the same value?

struct Foo {
    int x;
};

struct Bar : public Foo {
    int y;
};

#include <iostream>

int main ()
{
    Bar bar;

    Foo * f = &bar;
    Bar * b = &bar;
    std :: cout << f << " " << b << " " << (f == b) << "\n";

    void * fv = f;
    void * bv = b;
    std :: cout << fv << " " << bv << " " << (fv == bv) << "\n";

    int fi = reinterpret_cast <int> (f);
    int bi = reinterpret_cast <int> (b);
    std :: cout << fi << " " << bi << " " << (fi == bi) << "\n";
}
  • 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-18T09:08:39+00:00Added an answer on May 18, 2026 at 9:08 am

    About the only time that a base class object won’t have the same address as its subclass object is when multiple inheritance is involved.

    In the above example memory probably looks like this:

                                   / --------- \
                                  /  | x     |  > This is the Foo portion of bar
    This is the whole Bar object <   --------- /  
                                  \  | y     |  
                                   \ ---------  
    

    Both views of the object have the same starting point, so a pointer to either view will have the same value.

    In multiple inheritance, things get more complicated. Say you have:

    struct Foo1{ int x; };
    struct Foo2{ int y; };
    struct Bar : public Foo1, public Foo2 { int z; };
    Bar bar;
    

    Now the memory will have to be laid out something like this:

                                    / --------- \
                                   /  | x     |  > This is the Foo1 portion of bar
                                  /   --------- / \ 
    This is the whole Bar object <    | y     |    > This is the Foo2 portion of bar  
                                  \   ---------   /
                                   \  | z     |  
                                    \ ---------  
    

    So &bar and (Foo1*)&bar will have the same value, while (Foo2*)&bar will have a different value, since it the Foo2 portion of the object starts at a higher address.

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

Sidebar

Related Questions

When writing a switch statement, there appears to be two limitations on what you
Writing an operator< () for a struct appears to be clearer than writing the
I am having difficulty writing my code in the way it should be written.
While writing a test suite, I needed to provide an implementation of operator<<(std::ostream&... for
I'm writing a collection class. I want to overload the square brackets operator ([])
I'd like to be able to implicitly convert between two classes which are otherwise
Writing some test scripts in IronPython, I want to verify whether a window is
Writing the code for the user authentication portion of a web site (including account
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a

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.