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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Overloading and virtual functions

Overloading and virtual functions

Scheduled Pinned Locked Moved C / C++ / MFC
helptestingbeta-testingquestion
13 Posts 6 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.
  • B bob16972

    Thanks for the advice on how to cope with it. You don't by any chance know what is responsible, topic wise, for this behavior? Is this because of the concept of "Hiding"? I'm not sure what topic I need to google for to better understand it. Thanks

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

    bob16972 wrote:

    You don't by any chance know what is responsible, topic wise, for this behavior?

    CDerivedClass does not have a two-argument DoSomething() method, hence the compiler error.

    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

    B M 2 Replies Last reply
    0
    • D David Crow

      bob16972 wrote:

      You don't by any chance know what is responsible, topic wise, for this behavior?

      CDerivedClass does not have a two-argument DoSomething() method, hence the compiler error.

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      B Offline
      B Offline
      bob16972
      wrote on last edited by
      #5

      I guess I'm missing the point. I apologize if I'm missing something right under my nose. If your saying a derived class needs to implement all the virtual methods that are in the base, then I guess I'm not understanding why this works... class CBaseClass { public: CBaseClass(void) {} virtual ~CBaseClass(void) {} virtual void DoSomething(int a) {} virtual void DoSomething(int a, int b) {} }; class CDerivedClass : public CBaseClass { public: CDerivedClass(void) {} virtual ~CDerivedClass(void) {} }; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { CDerivedClass testing; testing.DoSomething(5,5); // Get an error on this line }

      D CPalliniC 2 Replies Last reply
      0
      • B bob16972

        Thanks for the advice on how to cope with it. You don't by any chance know what is responsible, topic wise, for this behavior? Is this because of the concept of "Hiding"? I'm not sure what topic I need to google for to better understand it. Thanks

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #6

        bob16972 wrote:

        Thanks for the advice on how to cope with it. You don't by any chance know what is responsible, topic wise, for this behavior? Is this because of the concept of "Hiding"? I'm not sure what topic I need to google for to better understand it.

        Polymorphism[^] Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class

        led mike

        B 1 Reply Last reply
        0
        • B bob16972

          I guess I'm missing the point. I apologize if I'm missing something right under my nose. If your saying a derived class needs to implement all the virtual methods that are in the base, then I guess I'm not understanding why this works... class CBaseClass { public: CBaseClass(void) {} virtual ~CBaseClass(void) {} virtual void DoSomething(int a) {} virtual void DoSomething(int a, int b) {} }; class CDerivedClass : public CBaseClass { public: CDerivedClass(void) {} virtual ~CDerivedClass(void) {} }; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { CDerivedClass testing; testing.DoSomething(5,5); // Get an error on this line }

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

          bob16972 wrote:

          If your saying a derived class needs to implement all the virtual methods that are in the base,

          They don't have to be implemented, but when they are, their types must match.

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • L led mike

            bob16972 wrote:

            Thanks for the advice on how to cope with it. You don't by any chance know what is responsible, topic wise, for this behavior? Is this because of the concept of "Hiding"? I'm not sure what topic I need to google for to better understand it.

            Polymorphism[^] Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class

            led mike

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #8

            thanks. I guess the part that is confusing me is why a pointer, at any level, will invoke the base implementations when no function is overloaded (and not implemented in a derived class) but a pointer at the base class is required when functions are overloaded and an implementation for at least one overloaded function is not provided in the derived class.

            1 Reply Last reply
            0
            • B bob16972

              I guess I'm missing the point. I apologize if I'm missing something right under my nose. If your saying a derived class needs to implement all the virtual methods that are in the base, then I guess I'm not understanding why this works... class CBaseClass { public: CBaseClass(void) {} virtual ~CBaseClass(void) {} virtual void DoSomething(int a) {} virtual void DoSomething(int a, int b) {} }; class CDerivedClass : public CBaseClass { public: CDerivedClass(void) {} virtual ~CDerivedClass(void) {} }; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { CDerivedClass testing; testing.DoSomething(5,5); // Get an error on this line }

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #9

              BTW, the following is allowed

              int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
              {
              CBaseClass *pCDerivedClass = new CDerivedClass();
              pCDerivedClass->DoSomething(5,5);
              }

              Please note: error check and cleanup left to the reader...

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              [my articles]

              In testa che avete, signor di Ceprano?

              B 1 Reply Last reply
              0
              • D David Crow

                bob16972 wrote:

                You don't by any chance know what is responsible, topic wise, for this behavior?

                CDerivedClass does not have a two-argument DoSomething() method, hence the compiler error.

                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #10

                hmm...what happened to inheritance? CDerivedClass is a CBaseClass. Logically, this doesn't seem like it should be an error. Until you convince me otherwise :) Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  BTW, the following is allowed

                  int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                  {
                  CBaseClass *pCDerivedClass = new CDerivedClass();
                  pCDerivedClass->DoSomething(5,5);
                  }

                  Please note: error check and cleanup left to the reader...

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  [my articles]

                  B Offline
                  B Offline
                  bob16972
                  wrote on last edited by
                  #11

                  Noted. :) I had become complacent with being able to call any function implemented in the base class regardless of whether it was virtual using a pointer from any level in the derived hierarchy when there were no overloaded functions in the base class. This kinda throws me curve ball but I'm learning. Thanks for the info.

                  1 Reply Last reply
                  0
                  • B bob16972

                    If I have one class derived from another, an overloaded virtual function in the base class, and an implementation of one of the overloaded functions in the derived class, the compiler will only let me call one of the overloaded functions from a pointer/reference of an instance of the derived class. I am confused as why it won't acknowledge the other function. Can anyone help me understand why this is? class CBaseClass { public: CBaseClass(void) {} virtual ~CBaseClass(void) {} virtual void DoSomething(int a) {} virtual void DoSomething(int a, int b) {} }; class CDerivedClass : public CBaseClass { public: CDerivedClass(void) {} virtual ~CDerivedClass(void) {} virtual void DoSomething(int a) {} }; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { CDerivedClass testing; testing.DoSomething(5,5); // Get an error on this line // error C2660: 'CDerivedClass::DoSomething' : function does not take 2 arguments }

                    B Offline
                    B Offline
                    Brian R
                    wrote on last edited by
                    #12

                    You can add a using statement to promote the visibility of the base class functions. class CDerivedClass : public CBaseClass { public: CDerivedClass(void) {} using CBaseClass::DoSomething; virtual ~CDerivedClass(void) {} virtual void DoSomething(int a) {} }; The eliminates the "hiding" problem.

                    B 1 Reply Last reply
                    0
                    • B Brian R

                      You can add a using statement to promote the visibility of the base class functions. class CDerivedClass : public CBaseClass { public: CDerivedClass(void) {} using CBaseClass::DoSomething; virtual ~CDerivedClass(void) {} virtual void DoSomething(int a) {} }; The eliminates the "hiding" problem.

                      B Offline
                      B Offline
                      bob16972
                      wrote on last edited by
                      #13

                      That seems to fix the issue. :) Thanks for the help. Now I need to go back and undo the workaround code and the half baked solution I put in.

                      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