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. General Programming
  3. C / C++ / MFC
  4. upcast

upcast

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    Smith
    wrote on last edited by
    #1

    is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }

    :beer:

    D A S 3 Replies Last reply
    0
    • S Smith

      is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }

      :beer:

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Smith# wrote:

      is there a way I can make this code say "I'm Base!"?

      You mean other than:

      CBase* cp = new CBase();

      There's also:

      void CDerived::testApp()
      {
      CBase::testApp();
      }

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

      A 1 Reply Last reply
      0
      • D David Crow

        Smith# wrote:

        is there a way I can make this code say "I'm Base!"?

        You mean other than:

        CBase* cp = new CBase();

        There's also:

        void CDerived::testApp()
        {
        CBase::testApp();
        }

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        You should have probably posted this as an answer instead of "general"

        1 Reply Last reply
        0
        • S Smith

          is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }

          :beer:

          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #4

          David is correct, that's the way to do it, if you don't like his options, then you probably shouldn't have made it virtual, if its not virtual, then the call gets evaluated at runtime according to the pointer. if not virtual...

          CBase* cp = new CDerived();
          cp->testApp(); //calls base testApp() because pointer is of type CBase
          ((CDerived *)cp)->testApp(); //calls derived testApp() because pointer is of type CDerived

          ..you just have to know exactly what it is you want.

          S 1 Reply Last reply
          0
          • S Smith

            is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }

            :beer:

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

            This should do it. Although I'm not at all sure why you'd want that.

            int _tmain(int argc, _TCHAR* argv[])
            {
            CBase* cp = new CDerived();
            cp->CBase::testApp();
            return 0;
            }

            S 1 Reply Last reply
            0
            • A Albert Holguin

              David is correct, that's the way to do it, if you don't like his options, then you probably shouldn't have made it virtual, if its not virtual, then the call gets evaluated at runtime according to the pointer. if not virtual...

              CBase* cp = new CDerived();
              cp->testApp(); //calls base testApp() because pointer is of type CBase
              ((CDerived *)cp)->testApp(); //calls derived testApp() because pointer is of type CDerived

              ..you just have to know exactly what it is you want.

              S Offline
              S Offline
              Smith
              wrote on last edited by
              #6

              So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?

              :beer:

              A S 3 Replies Last reply
              0
              • S Smith

                So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?

                :beer:

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                Its not dead, its just the whole purpose behind the "virtual" keyword... that the base method can be overriden... stefan's solution probably works as well, but again, you're overriding a behavior that is designed.

                1 Reply Last reply
                0
                • S Smith

                  So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?

                  :beer:

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #8

                  ...and by the way, the derived class doesn't HAVE to override a virtual method in the base, its optional, you only HAVE to override pure virtuals.

                  1 Reply Last reply
                  0
                  • S Stefan_Lang

                    This should do it. Although I'm not at all sure why you'd want that.

                    int _tmain(int argc, _TCHAR* argv[])
                    {
                    CBase* cp = new CDerived();
                    cp->CBase::testApp();
                    return 0;
                    }

                    S Offline
                    S Offline
                    Smith
                    wrote on last edited by
                    #9

                    because the intellisense hides this from me.

                    :beer:

                    A 1 Reply Last reply
                    0
                    • S Smith

                      So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?

                      :beer:

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #10

                      See my answer below: you can still call methods from the base class(es) even if the pointer you use to call it points to a class that has overridden that implementation:

                      CBase* pbase = new CBase;
                      CDerived* pDerived = new CDerived;
                      pbase->testApp(); // "I'm Base"
                      pDerived->testApp(); // I'm Derived"
                      pDerived->CBase::testApp(); // "I'm Base"

                      You can invoke methods of base classes by adding the name of that class, separated by '::' to the name of the function you want to call. In doing so you ignore the virtual function table for that call only. You can freely mix calls of that type with normal calls that use the virtual functions, as you can see in the example above.

                      1 Reply Last reply
                      0
                      • S Smith

                        because the intellisense hides this from me.

                        :beer:

                        A Offline
                        A Offline
                        Albert Holguin
                        wrote on last edited by
                        #11

                        this isn't what you'd call normal use...

                        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