Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Getting the hierarchy wrong

Getting the hierarchy wrong

Scheduled Pinned Locked Moved Clever Code
7 Posts 5 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Brainley
    wrote on last edited by
    #1

    Here it comes :

    class A
    {
    public:
    virtual void run() = 0;
    };

    class B :
    public class A,
    public SomeOtherClass
    {
    public:
    virtual void run();
    }

    That's in the headers. Now an excerpt from main :

    SomeOtherClass *dummy = new B();

    reinterpret_cast<A *>(dummy)->run();

    This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.

    M M Z 3 Replies Last reply
    0
    • M Mr Brainley

      Here it comes :

      class A
      {
      public:
      virtual void run() = 0;
      };

      class B :
      public class A,
      public SomeOtherClass
      {
      public:
      virtual void run();
      }

      That's in the headers. Now an excerpt from main :

      SomeOtherClass *dummy = new B();

      reinterpret_cast<A *>(dummy)->run();

      This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      reinterpret_cast is the cast of absolute last resort, and it simply converts the raw bits from one format to another. In this case it will simply call the function in SomeOtherClass that occupies the same position in the vtable that run does in A (in your example, the first one). Because you need to cast down the hierarchy and you don't know whether the most-derived class that dummy actually points to derives from A, you need to use a dynamic_cast. The fact that dummy points to a B is lost immediately after the assignment occurs.

      Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      • M Mr Brainley

        Here it comes :

        class A
        {
        public:
        virtual void run() = 0;
        };

        class B :
        public class A,
        public SomeOtherClass
        {
        public:
        virtual void run();
        }

        That's in the headers. Now an excerpt from main :

        SomeOtherClass *dummy = new B();

        reinterpret_cast<A *>(dummy)->run();

        This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        That's a cross-cast, no? Wouldn't you need to cast from SomeOtherClass* to B* and then to A* ?

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        1 Reply Last reply
        0
        • M Mr Brainley

          Here it comes :

          class A
          {
          public:
          virtual void run() = 0;
          };

          class B :
          public class A,
          public SomeOtherClass
          {
          public:
          virtual void run();
          }

          That's in the headers. Now an excerpt from main :

          SomeOtherClass *dummy = new B();

          reinterpret_cast<A *>(dummy)->run();

          This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          This is part of the reason why multiple inheritance should be avoided.

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          S 1 Reply Last reply
          0
          • Z Zac Howland

            This is part of the reason why multiple inheritance should be avoided.

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            S Offline
            S Offline
            sbeckstead
            wrote on last edited by
            #5

            Sorry have to reply to your sig... You only work half time?

            Z 1 Reply Last reply
            0
            • S sbeckstead

              Sorry have to reply to your sig... You only work half time?

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              :laugh: Well, guess I should add "at a minimum" to that. Though, where I work now isn't nearly as bad as where I use to work. I work about 41 hours a week instead of 70 ... and I get paid more.

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              S 1 Reply Last reply
              0
              • Z Zac Howland

                :laugh: Well, guess I should add "at a minimum" to that. Though, where I work now isn't nearly as bad as where I use to work. I work about 41 hours a week instead of 70 ... and I get paid more.

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                S Offline
                S Offline
                sbeckstead
                wrote on last edited by
                #7

                I took a job during the .com bubble and calculated my hourly wage the first month as less than minimum wage. I look for much more stable work environments lately. The place I'm at now is pretty good. But we digress. Scott

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups