473,385 Members | 1,848 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,385 software developers and data experts.

invoke sub-class's derived virtual method.

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():P(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
Jul 23 '05 #1
2 1997
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():P(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():P(this) {}
int pure() { cout << "derived method is called\n"; }
~D() {}
};

int main()
{
D *d = new D();
delete d;
}
Jul 23 '05 #2

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

Similar topics

1
by: Dave Wurtz | last post by:
All, I've been trying to use MethodBase.Invoke(...) to dynamically call a sub at runtime. If I change the Sub to Function, I can get it to work. Is it possible to call a Sub from Invoke? ...
1
by: Thai Mai Shu | last post by:
What is wrong with my call below. If I change the delegate and the CallBackComplete function to not take in parameters then the .Invoke call works fine. As soon as I put the parameters back I...
6
by: Wayne Wengert | last post by:
I have a Windows application in which users can select one or more individuals and an email message is created which they then complete and send. I currently use MAPI for this but I want to change...
2
by: Lucvdv | last post by:
To avoid a temporarily frozen user interface, I'm using a separate thread to fill a list with items found in a database (there can be from a few up to about 1000 or 1500 items). There seems to...
1
by: yxq | last post by:
Hello, The sample code is below ************************************** Private sub invokeEvent() 'Here i want to invoke all the codes(A,B,C) in Timer1_Tick(), how to do? End Sub
1
by: Dzemo | last post by:
I got this code to add TreeNode from my new Thread to TreeView /// Private Delegate Function AddDelegate( ByVal text As String ) As TreeNode Private Sub DoWork() Me.TreeView1.Invoke( New...
9
by: Terry Olsen | last post by:
I'm running an asynchronous Socket. In the ReceiveCallback method, I need to append what is received to a textbox on the main form. I have this code: Private Sub ToChatWindow(ByVal msg As...
1
by: swartzbill2000 | last post by:
Hello, I wish to use Control.Invoke to make an inter-thread call to a sub that has ByRef args. How can I pass the ByRef args? Syntax like New Object() {byRefArg1, byRefArg2} in fact passes by...
2
by: andrewbb | last post by:
This is fairly simple code, what am I missing or is there a potential workaround? It fails when passing someParms to Invoke with "Parameter count mismatch". I created a test with an empty form...
0
by: Manfred | last post by:
Hello I'm new to VB.Net and try to create a console applicatione which will redirect the output to an arraylist. I found a very good sample at:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.