Adding new overloaded function hides base class function
-
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 ;PHans
-
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 ;PHans
-
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 ;PHans
D::func
hidesC::func
. You actually have to bring all the functions fromC
intoD
to have the new function overload the old ones. This is the standard-compliant behaviour. In a standard-compliant compiler, you can place the declarationusing 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
-
D::func
hidesC::func
. You actually have to bring all the functions fromC
intoD
to have the new function overload the old ones. This is the standard-compliant behaviour. In a standard-compliant compiler, you can place the declarationusing 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
Mike Dimmick wrote:
using C::f;
I think just plain
C::f;
is enough. I may be wrong tho.
:badger:
-
Mike Dimmick wrote:
using C::f;
I think just plain
C::f;
is enough. I may be wrong tho.
:badger:
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
-
Mike Dimmick wrote:
using C::f;
I think just plain
C::f;
is enough. I may be wrong tho.
:badger:
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