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. Adding new overloaded function hides base class function

Adding new overloaded function hides base class function

Scheduled Pinned Locked Moved Clever Code
regexhelptutorial
6 Posts 4 Posters 2 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.
  • H Offline
    H Offline
    Hans
    wrote on last edited by
    #1

    We had to add an additional bool parameter to a function (simplified example) class C { public:   void func(const char* c,const char* d) { printf("C func(const char*) called\n"); } }; class D : public C { public:   // --> this is our added overload   void func(const char* c,bool b) { printf("D func(bool b) called\n"); } }; To our surprise from now for calls with two strings our new function came to play, not the base class function anymore (and no compile time warning either): int main(int argc, char* argv[]) {   D d;   // --> this calls func(const char* c,bool b) not func(const char* c,const char* d)   d.func("foo","bar");   return 0; } I'm not sure why the compiler prefers the conversion of second argument to bool, instead of using the perfect match from the base class :confused:. But the fix was simple, rename the function to something different ;P

    Hans

    R M 2 Replies Last reply
    0
    • H Hans

      We had to add an additional bool parameter to a function (simplified example) class C { public:   void func(const char* c,const char* d) { printf("C func(const char*) called\n"); } }; class D : public C { public:   // --> this is our added overload   void func(const char* c,bool b) { printf("D func(bool b) called\n"); } }; To our surprise from now for calls with two strings our new function came to play, not the base class function anymore (and no compile time warning either): int main(int argc, char* argv[]) {   D d;   // --> this calls func(const char* c,bool b) not func(const char* c,const char* d)   d.func("foo","bar");   return 0; } I'm not sure why the compiler prefers the conversion of second argument to bool, instead of using the perfect match from the base class :confused:. But the fix was simple, rename the function to something different ;P

      Hans

      R Offline
      R Offline
      ricecake
      wrote on last edited by
      #2

      This issue is actually addressed in the C++ FAQ: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9[^]. It has workarounds that allow you to continue using the same name for the functions.

      -- Marcus Kwok

      1 Reply Last reply
      0
      • H Hans

        We had to add an additional bool parameter to a function (simplified example) class C { public:   void func(const char* c,const char* d) { printf("C func(const char*) called\n"); } }; class D : public C { public:   // --> this is our added overload   void func(const char* c,bool b) { printf("D func(bool b) called\n"); } }; To our surprise from now for calls with two strings our new function came to play, not the base class function anymore (and no compile time warning either): int main(int argc, char* argv[]) {   D d;   // --> this calls func(const char* c,bool b) not func(const char* c,const char* d)   d.func("foo","bar");   return 0; } I'm not sure why the compiler prefers the conversion of second argument to bool, instead of using the perfect match from the base class :confused:. But the fix was simple, rename the function to something different ;P

        Hans

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

        D::func hides C::func. You actually have to bring all the functions from C into D to have the new function overload the old ones. This is the standard-compliant behaviour. In a standard-compliant compiler, you can place the declaration

        using C::f;

        into the body of D to have those functions included by reference. Source: "Exceptional C++" by Herb Sutter, Item 21, Item 34.

        Stability. What an interesting concept. -- Chris Maunder

        A 1 Reply Last reply
        0
        • M Mike Dimmick

          D::func hides C::func. You actually have to bring all the functions from C into D to have the new function overload the old ones. This is the standard-compliant behaviour. In a standard-compliant compiler, you can place the declaration

          using C::f;

          into the body of D to have those functions included by reference. Source: "Exceptional C++" by Herb Sutter, Item 21, Item 34.

          Stability. What an interesting concept. -- Chris Maunder

          A Offline
          A Offline
          Anton Afanasyev
          wrote on last edited by
          #4

          Mike Dimmick wrote:

          using C::f;

          I think just plain C::f; is enough. I may be wrong tho.


          :badger:

          R M 2 Replies Last reply
          0
          • A Anton Afanasyev

            Mike Dimmick wrote:

            using C::f;

            I think just plain C::f; is enough. I may be wrong tho.


            :badger:

            R Offline
            R Offline
            ricecake
            wrote on last edited by
            #5

            No, Mike is right. You need the using declaration. However, if you are directly calling the base class function in the context of actual use, then you don't need the using declaration. See my link to the C++ FAQ in my other post for more information.

            -- Marcus Kwok

            1 Reply Last reply
            0
            • A Anton Afanasyev

              Mike Dimmick wrote:

              using C::f;

              I think just plain C::f; is enough. I may be wrong tho.


              :badger:

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

              Probably depends on how strict your compiler is! Just mentioning the name was probably sufficient for some pre-standard and less-conformant compilers, but the standard says to use using.

              Stability. What an interesting concept. -- Chris Maunder

              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