virtual function definition

N

newbie

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?

Thanks

//class.h
class AbstractFoo {
public:
virtual void foo() = 0;
}

class A_Foo : public AbstractFoo{
public:
virtual void foo();
void A_func() { return; }
}

class B_Foo : public AbstractFoo {
public:
void B_func() {return;}
}

//class.cc
void A_Foo::foo() {
// ....
}

void B_Foo::foo(){
//...
}
 
A

Alan Johnson

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?

Thanks

//class.h
class AbstractFoo {
public:
virtual void foo() = 0;

}

class A_Foo : public AbstractFoo{
public:
virtual void foo();
void A_func() { return; }

}

class B_Foo : public AbstractFoo {
public:
void B_func() {return;}

}

//class.cc
void A_Foo::foo() {
// ....

}

void B_Foo::foo(){
//...

}


You have at least one incorrect assumption. When you override a
virtual member function, you do indeed need to declare it in the
derived class. Perhaps you are confused by the fact that you do not
need to declare it as virtual (it will be virtual whether you declare
as such or not). That is, the following two class definitions would
be exactly the same:

class B_Foo : public AbstractFoo {
public:
void foo();
void B_func() {return;}
};

class B_Foo : public AbstractFoo {
public:
virtual void foo();
void B_func() {return;}
};


My personal preference is to include the virtual keyword even though
it is not necessary, for self-documenting purposes.
 
A

Andre Kostur

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!
 
N

newbie

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?
 
A

Andre Kostur

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?
 
N

newbie

First... post compilable code.


This function has no return type. Not valid C++.


This variable has no type at all. Not valid C++.



Same as above.


Yep. Default values don't affect the function's signature.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,163
Latest member
NicolasMcL

Latest Threads

Top