Re: virtual function definition
Thanks for the clear answer.
another question about function signature.
class AbstractBar {
public:
virtual Bar(string text) = 0;
private:
_text;
}
class ConcreteBar : public AbstractBar {
public:
virtual Bar(string text ="") { _text = text; } //(1)
private:
_text; //(2)
}
I have two questions here:
(1) does 'virtual Bar(string text ="")' have same signature as the one
declared in AbstractBar? i.e., whether that functions override the
interface Bar(string text) declared in AbstractBar.
(2) does _text declaration allowed at all? I guess not. But if so, how
can children classes (derived from ConcreteBar) know there exists such
a variable? --the only way to carry such information is through
document?
On Jun 26, 4:38 pm, Andre Kostur <nntps...@kostur.net> wrote:
newbie <mitbb...@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!