Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > invoke sub-class's derived virtual method.

Reply
Thread Tools

invoke sub-class's derived virtual method.

 
 
sam++
Guest
Posts: n/a
 
      04-12-2005
Hi,

I created a pure virtual class P and its derived sub-class D.
In class P, declared a pure virtural function pure(); in sub-class D
define its implementation.

eg.

class P
{
public:
P(P *p) { cout << p->pure(); }
virtual pure() = 0;
~virtual P() {}
}

class D: public P
{
public:
D()(this) {}
pure() { cout << "derived method is called\n";
~D() {}
}

int main()
{
D *d = new D();
}

I would expect after compiled this program, it will print out the
following result:
"derived method is called"

But it tried to invoke the pure virtual function.
How can I correct this error? For some reason, I don't want to call
d->pure() in the main() function.

Thanks
Sam
 
Reply With Quote
 
 
 
 
sam++
Guest
Posts: n/a
 
      04-12-2005
sam++ wrote:

> Hi,
>
> I created a pure virtual class P and its derived sub-class D.
> In class P, declared a pure virtural function pure(); in sub-class D
> define its implementation.
>
> eg.
>
> class P
> {
> public:
> P(P *p) { cout << p->pure(); }
> virtual pure() = 0;
> ~virtual P() {}
> }
>
> class D: public P
> {
> public:
> D()(this) {}
> pure() { cout << "derived method is called\n";
> ~D() {}
> }
>
> int main()
> {
> D *d = new D();
> }
>
> I would expect after compiled this program, it will print out the
> following result:
> "derived method is called"
>
> But it tried to invoke the pure virtual function.
> How can I correct this error? For some reason, I don't want to call
> d->pure() in the main() function.
>
> Thanks
> Sam


Sorry, I just some syntax error. Please see the fixed version of the
above example. Just copy to your system and compile it with g++. The
program will invoke the pure virtual function, hence caused core dump.

#include <iostream>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

class P
{
public:
P(P *p) { p->pure(); }
virtual int pure() = 0;
virtual ~P() {}
};

class D: public P
{
public:
D()(this) {}
int pure() { cout << "derived method is called\n"; }
~D() {}
};

int main()
{
D *d = new D();
delete d;
}
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-12-2005
http://www.parashift.com/c++-faq-lit....html#faq-10.7


--
John Carson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
If declared as virtual in base, its derived version also is virtual. Why not for destructors? qazmlp C++ 7 07-27-2004 03:10 PM
What will happen if a virtual function is declared to be virtual again in a derived class? Xiangliang Meng C++ 2 06-21-2004 03:14 AM



Advertisments