Sunday 15 May 2011

c++ - Alternate argument lists of pure virtual won't inherit (needed "using") -



c++ - Alternate argument lists of pure virtual won't inherit (needed "using") -

i've got base of operations class few pure virtuals, , want provide default implementations of several methods can overridden if needed. i've boiled problem downwards following:

class {}; class b {}; class base of operations { public: virtual void foo (const a&) {}; virtual void foo (const b&) = 0; }; class derived : public base of operations { public: virtual void foo(const b&) {}; }; int main(int argv, char** argc) { derived d; d.foo(a()); homecoming 0; }

for reasons don't understand @ all, fails on g++ 4.8.2:

g++ scratch.cpp -o scratch scratch.cpp: in function ‘int main(int, char**)’: scratch.cpp:17:12: error: no matching function phone call ‘derived::foo(a)’ d.foo(a()); ^ scratch.cpp:17:12: note: candidate is: scratch.cpp:12:16: note: virtual void derived::foo(const b&) virtual void foo(const b&) {}; ^ scratch.cpp:12:16: note: no known conversion argument 1 ‘a’ ‘const b&’

if define foo(const b&) in base , leave derived empty, problem goes away. i've read docs , of questions here, nil has suggested why problem. in real code, base::foo(const a&) build special object b of type b a object, , phone call foo(b).

add line derived class definition:

using base::foo;

this solve problem foo(b) definition in derived hides both foo(a) , foo(b) functions in base.

c++ inheritance pure-virtual

No comments:

Post a Comment