473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

virtual function definition

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(){
//...
}

Jun 26 '07 #1
1 1279
newbie <mi******@yahoo.comwrote 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!

Jun 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dennis | last post by:
Hi, This may be a simple answer for somebody, but I'm unable to figure it out. In a nutshell: I have a base class defined. Let's call it XClient. Inside XClient I have a virtual method...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
12
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
by: trond.norbye | last post by:
Hi I am trying to compile a quite large software system with a new compiler. During that attempt I am facing problems during the link phase, and I have traced it down to the following construct:...
15
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
3
by: Rahul | last post by:
Hi Everyone, I have the following polymorphic classes, class Shape { public : virtual void draw() { } virtual void sample();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.