Re: virtual function definition
On Jun 29, 11:44 am, Andre Kostur <nntps...@kostur.net> wrote:
newbie <mitbb...@yahoo.com> wrote innews:1183141930.843157.103030@e9g2000prf.googlegroups.com:
Thanks for the clear answer.
another question about function signature.
First... post compilable code.
class AbstractBar {
public:
virtual Bar(string text) = 0;
This function has no return type. Not valid C++.
private:
_text;
This variable has no type at all. Not valid C++.
}
class ConcreteBar : public AbstractBar {
public:
virtual Bar(string text ="") { _text = text; } //(1)
private:
_text; //(2)
}
Same as above.
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.
Yep. Default values don't affect the function's signature.
(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?
Given that _text is private in AbstractBar (and ConcreteBar for that
matter), ConcreteBar doesn't have access to it anyway (or any of its
descendants).
And why wouldn't the children know that _text exists (even though they
can't access it)? It's in the header file. Where else would you expect
to find it?
Suppose AbstractBar is in abstractbar.h, and ConcreteBar is in
concretebar.h (say they are in different directories).
child_of_concretebar.h defining the child class of ConcreteBar is in
the same directory of concretebar.h. Thus, it requires the developer
of child_of_concretebar.h to read abstractbar.h (and possibly in a
different directory) to see things underhood.
That's my point. I am not sure if there exist some 'standard way' to
ease such a procedure.
Thanks