T***B 发帖数: 137 | 1 When I compile the following code with g++, I got following error:
g++ tt.cpp
tt.cpp: In static member function `static void Derived::test()':
tt.cpp:36: no matching function for call to `Derived::foo(int)'
tt.cpp:23: candidates are: virtual int Derived::foo(int, int)
Seems the compiler doesn't try to look for the foo with 2 paras in the base
class if there is one foo method available in derived class. Any reason
compiler chooses to do this this way. How to make this thing work (elegantly
)?
Tha |
h****e 发帖数: 2125 | 2 the compiler expects you to override virtual foo(int) method as well,
otherwise it's confused. just simply do in Derived:
"
virtual int foo(int i)
{
return Base::foo(i);
}
"
base
elegantly
【在 T***B 的大作中提到】 : When I compile the following code with g++, I got following error: : g++ tt.cpp : tt.cpp: In static member function `static void Derived::test()': : tt.cpp:36: no matching function for call to `Derived::foo(int)' : tt.cpp:23: candidates are: virtual int Derived::foo(int, int) : Seems the compiler doesn't try to look for the foo with 2 paras in the base : class if there is one foo method available in derived class. Any reason : compiler chooses to do this this way. How to make this thing work (elegantly : )? : Tha
|
o*o 发帖数: 404 | 3 TIC C. 14 Name Hiding.
base
elegantly
【在 T***B 的大作中提到】 : When I compile the following code with g++, I got following error: : g++ tt.cpp : tt.cpp: In static member function `static void Derived::test()': : tt.cpp:36: no matching function for call to `Derived::foo(int)' : tt.cpp:23: candidates are: virtual int Derived::foo(int, int) : Seems the compiler doesn't try to look for the foo with 2 paras in the base : class if there is one foo method available in derived class. Any reason : compiler chooses to do this this way. How to make this thing work (elegantly : )? : Tha
|
T***B 发帖数: 137 | 4 Thanks 坚强. Your solution works. But I may use following approach instead
to make it cleaner.
virtual int foo(int i, int j = 0); |
t****t 发帖数: 6806 | 5 why not write
using Base::foo;
in Derived?
【在 h****e 的大作中提到】 : the compiler expects you to override virtual foo(int) method as well, : otherwise it's confused. just simply do in Derived: : " : virtual int foo(int i) : { : return Base::foo(i); : } : " : : base
|
h****e 发帖数: 2125 | 6 i've no problem with that.
【在 t****t 的大作中提到】 : why not write : using Base::foo; : in Derived?
|
T***B 发帖数: 137 | 7 I feel it's not natural for the method of the derived class to do nothing
but calling the Base::foo.
virtual int foo(int i)
{
return Base::foo(i);
} |