Re: virtual function definition
newbie <mitbbsmj@yahoo.com> wrote in news:1182898648.336447.35930
@d30g2000prg.googlegroups.com:
Need a virtual function be redeclared in child class? I thought it's
not necessary, but g++ compiler complains that I didn't declare foo()
in B_Foo. I thought I can optionally do it because AbstractFoo already
does so. am I right?
Virtuals must be redeclared in a child class? Not necessarily. However,
your foo() function is a _pure_ virtual function, so it must be
redeclared.
Thanks
//class.h
class AbstractFoo {
public:
virtual void foo() = 0;
}
class A_Foo : public AbstractFoo{
public:
virtual void foo();
void A_func() { return; }
}
OK, A_Foo declares a foo() method, so A_Foo will be instantiatable (you
can create a variable of this type).
class B_Foo : public AbstractFoo {
public:
void B_func() {return;}
}
B_Foo does not declare a foo() method, so B_Foo will not be
instantiatable. Hopefully sometime in the future B_Foo will have a child
class which will declare a foo().
//class.cc
void A_Foo::foo() {
// ....
}
void B_Foo::foo(){
//...
}
The compiler let you do this? B_Foo::foo() doesn't exist!