pointer equality and inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sb

    pointer equality and inheritance

    Given this relationship,

    class Base {
    //...
    };


    class Derived : public Base {
    //...
    bool myself(const void* p) { return p == this}
    };



    Is there a guarantee in the standard that

    Derived d;
    Base* p = &d;
    d.myself((void* )p); // - ?

    will be always true?
  • Pete Vidler

    #2
    Re: pointer equality and inheritance

    sb wrote:
    [snip][color=blue]
    > Is there a guarantee in the standard that
    >
    > Derived d;
    > Base* p = &d;
    > d.myself((void* )p); // - ?
    >
    > will be always true?[/color]

    I'm almost certain that there isn't. I've been bitten by this before.

    -- Pete

    Comment

    • Leor Zolman

      #3
      Re: pointer equality and inheritance

      On 1 Apr 2004 13:28:43 -0800, spam_bait101@ya hoo.com (sb) wrote:
      [color=blue]
      >Given this relationship,
      >
      >class Base {
      > //...
      >};
      >
      >
      >class Derived : public Base {
      > //...
      > bool myself(const void* p) { return p == this}
      >};
      >
      >
      >
      >Is there a guarantee in the standard that
      >
      >Derived d;
      >Base* p = &d;
      >d.myself((void *)p); // - ?
      >
      >will be always true?[/color]

      Adding a bit to your example, if you try this the assertion will fail.
      Granted this isn't the same exact thing you posted, but I think it might be
      helpful to know that multiple inheritance being involved will trigger the
      assertion failure.
      -leor

      #include <iostream>
      #include <cassert>
      using namespace std;

      class Base {
      //...
      };


      class Base2 {};

      class Derived : public Base2, public Base {
      //...
      public:
      bool myself(const void* p) { return p == this;}
      };

      int main()
      {
      Derived d;
      Base* p = &d;
      assert(d.myself ((void*)p)); // - ?

      return 0;
      }

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • John Harrison

        #4
        Re: pointer equality and inheritance


        "sb" <spam_bait101@y ahoo.com> wrote in message
        news:221dd125.0 404011328.4f48d e15@posting.goo gle.com...[color=blue]
        > Given this relationship,
        >
        > class Base {
        > //...
        > };
        >
        >
        > class Derived : public Base {
        > //...
        > bool myself(const void* p) { return p == this}
        > };
        >
        >
        >
        > Is there a guarantee in the standard that
        >
        > Derived d;
        > Base* p = &d;
        > d.myself((void* )p); // - ?
        >
        > will be always true?[/color]

        No.

        class Base
        {
        };

        class Derived : public Base
        {
        public:
        virtual ~Derived() {}
        bool myself(const void* p) { return p == this; }
        };

        int main()
        {
        Derived d;
        Base* p = &d;
        cout << (d.myself(p) ? "true\n" : "false\n");
        }

        prints false on my system. The virtual destructor in Derived but not in Base
        is what spoils things.

        john


        Comment

        • Nick Hounsome

          #5
          Re: pointer equality and inheritance


          "sb" <spam_bait101@y ahoo.com> wrote in message
          news:221dd125.0 404011328.4f48d e15@posting.goo gle.com...[color=blue]
          > Given this relationship,
          >
          > class Base {
          > //...
          > };
          >
          >
          > class Derived : public Base {
          > //...
          > bool myself(const void* p) { return p == this}
          > };
          >
          >
          >
          > Is there a guarantee in the standard that
          >
          > Derived d;
          > Base* p = &d;
          > d.myself((void* )p); // - ?[/color]

          You dont need the cast
          [color=blue]
          >
          > will be always true?[/color]

          No for the reasons others have given but why would you compare a void
          pointer anyway?
          Why not do the sensible thing:

          d == p;

          If you don't actually have a Base* (which makes your example incorrect) then
          you MUST
          make sure that the void pointer always pointed to the same sort of thing
          then you can cast back and compare:

          Derived d; // general case derived in complex way
          Base b;
          Unrelated u; // If you are not storing some unrelated class
          //as well then you should be using Base* not void*
          void* p[3] = {static_cast<Ba se*>(&d), &b, &u };

          assert(&d == static_cast<Bas e*>(p[0]));
          assert(&b == static_cast<Bas e*>(p[1]));
          assert(&d != static_cast<Bas e*>(p[2]));


          Comment

          • Nick Hounsome

            #6
            Re: pointer equality and inheritance


            "sb" <spam_bait101@y ahoo.com> wrote in message
            news:221dd125.0 404011328.4f48d e15@posting.goo gle.com...[color=blue]
            > Given this relationship,
            >
            > class Base {
            > //...
            > };
            >
            >
            > class Derived : public Base {
            > //...
            > bool myself(const void* p) { return p == this}
            > };
            >
            >
            >
            > Is there a guarantee in the standard that
            >
            > Derived d;
            > Base* p = &d;
            > d.myself((void* )p); // - ?[/color]

            You dont need the cast
            [color=blue]
            >
            > will be always true?[/color]

            No for the reasons others have given but why would you compare a void
            pointer anyway?
            Why not do the sensible thing:

            d == p;

            If you don't actually have a Base* (which makes your example incorrect) then
            you MUST
            make sure that the void pointer always pointed to the same sort of thing
            then you can cast back and compare:

            Derived d; // general case derived in complex way
            Base b;
            Unrelated u; // If you are not storing some unrelated class
            //as well then you should be using Base* not void*
            void* p[3] = {static_cast<Ba se*>(&d), &b, &u };

            assert(&d == static_cast<Bas e*>(p[0]));
            assert(&b == static_cast<Bas e*>(p[1]));
            assert(&d != static_cast<Bas e*>(p[2]));


            Comment

            Working...