473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cast from void* to base class virtual function problem

Code: Thread -> U
-> T

public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;

public:
// Static entry function for the internal thread
static void* ThreadEntry(voi d* pThread) {
Thread* thread = static_cast<Thr ead*>(pThread);

// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}

// Starts the internal thread running
void Start() {
pthread_create( &_tid, NULL, Thread::ThreadE ntry, this);
}
}

public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}

public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}

As you can see I'm just trying to re-use some pthread code for a
multithreaded server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...

Of course, the standard usage like

Thread* u = new U();
u->foo();

calls the derived foo like I expect.

I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.

I'm on OSX 10.4 development version. Not sure which gcc it is.

Thanks in advance,
Luke
Sep 18 '05 #1
5 2999

Luke Dalessandro wrote:
Code: Thread -> U
-> T

public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;

public:
// Static entry function for the internal thread
static void* ThreadEntry(voi d* pThread) {
Thread* thread = static_cast<Thr ead*>(pThread);

// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}

// Starts the internal thread running
void Start() {
pthread_create( &_tid, NULL, Thread::ThreadE ntry, this);
}
}

public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}

public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}
As you can see I'm just trying to re-use some pthread code for a
multithreaded server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...

Of course, the standard usage like

Thread* u = new U();
u->foo();

calls the derived foo like I expect.

I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.

I'm on OSX 10.4 development version. Not sure which gcc it is.

Thanks in advance,
Luke


The code that allocates the Thread object for each pthread created is
missing from your post. In fact there is no obvious execution order to
the code that was posted.

Without knowing what pThread is actually pointing to, it's not possible
to fully diagnose the problem. It's also possible that there is no code
in the program to create new Thread objects, in which case the same
pThread pointer is being passed to every pthread.

Greg

Sep 18 '05 #2

Luke Dalessandro wrote:
Code: Thread -> U
-> T

public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;

public:
// Static entry function for the internal thread
static void* ThreadEntry(voi d* pThread) {
Thread* thread = static_cast<Thr ead*>(pThread);

// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}

// Starts the internal thread running
void Start() {
pthread_create( &_tid, NULL, Thread::ThreadE ntry, this);
}
}

public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}

public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}

As you can see I'm just trying to re-use some pthread code for a
multithreaded server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...

Of course, the standard usage like

Thread* u = new U();
u->foo();

calls the derived foo like I expect.

I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.

I'm on OSX 10.4 development version. Not sure which gcc it is.

Thanks in advance,
Luke


In C++, you don't put key word public before a class declaration.

Sep 18 '05 #3


Greg wrote:
Luke Dalessandro wrote:
Code: Thread -> U
-> T

public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;

public:
// Static entry function for the internal thread
static void* ThreadEntry(voi d* pThread) {
Thread* thread = static_cast<Thr ead*>(pThread);

// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}

// Starts the internal thread running
void Start() {
pthread_create( &_tid, NULL, Thread::ThreadE ntry, this);
}
}

public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}

public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}

As you can see I'm just trying to re-use some pthread code for a
multithread ed server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...

Of course, the standard usage like

Thread* u = new U();
u->foo();

calls the derived foo like I expect.

I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.

I'm on OSX 10.4 development version. Not sure which gcc it is.

Thanks in advance,
Luke

The code that allocates the Thread object for each pthread created is
missing from your post. In fact there is no obvious execution order to
the code that was posted.

Without knowing what pThread is actually pointing to, it's not possible
to fully diagnose the problem. It's also possible that there is no code
in the program to create new Thread objects, in which case the same
pThread pointer is being passed to every pthread.

Greg


Greg,

Sorry about the lack, I was trying to remove everything that didn't seem
necessary. The code is basically:

int main (int argc, const * char argv[]) {

string toExit;
Thread* t;

while (toExit != "exit") {

cin >> toExit;

if (toExit = "U") {
t = new U();
}
else {
t = new V();
}

t->Start();

delete t;

}

}

Constructors are:

Thread::Thread( ) {}
U::U() {}
V::V() {}

Destructors are:

virtual ~Thread() {}
virtual ~U() {}
virtual ~V() {}

I realize that the main thread s pulling the rug out from under the
pthreads that are running inside the Thread objects... I'm actually
storing the pointers somewhere and joining and deleting threads elsewhere.

Luke
Sep 18 '05 #4


Axter wrote:
Luke Dalessandro wrote:
Code: Thread -> U
-> T

public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;

public:
// Static entry function for the internal thread
static void* ThreadEntry(voi d* pThread) {
Thread* thread = static_cast<Thr ead*>(pThread);

// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}

// Starts the internal thread running
void Start() {
pthread_create( &_tid, NULL, Thread::ThreadE ntry, this);
}
}

public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}

public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}

As you can see I'm just trying to re-use some pthread code for a
multithread ed server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...

Of course, the standard usage like

Thread* u = new U();
u->foo();

calls the derived foo like I expect.

I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.

I'm on OSX 10.4 development version. Not sure which gcc it is.

Thanks in advance,
Luke

In C++, you don't put key word public before a class declaration.


Lol... you're right... I've been stuck in C# land for too long (language
at work). Thanks for pointing that out... I didn't cut and paste the
code, I just re-wrote it in simplified form, so there was no compiler
around to yell at men.

Luke
Sep 18 '05 #5


Luke Dalessandro wrote:


Greg wrote:
Luke Dalessandro wrote:
Code: Thread -> U
-> T

public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;

public:
// Static entry function for the internal thread
static void* ThreadEntry(voi d* pThread) {
Thread* thread = static_cast<Thr ead*>(pThread);

// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}

// Starts the internal thread running
void Start() {
pthread_create( &_tid, NULL, Thread::ThreadE ntry, this);
}
}

public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}

public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}


As you can see I'm just trying to re-use some pthread code for a
multithreaded server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...

Of course, the standard usage like

Thread* u = new U();
u->foo();

calls the derived foo like I expect.

I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.

I'm on OSX 10.4 development version. Not sure which gcc it is.

Thanks in advance,
Luke


The code that allocates the Thread object for each pthread created is
missing from your post. In fact there is no obvious execution order to
the code that was posted.

Without knowing what pThread is actually pointing to, it's not possible
to fully diagnose the problem. It's also possible that there is no code
in the program to create new Thread objects, in which case the same
pThread pointer is being passed to every pthread.

Greg


Greg,

Sorry about the lack, I was trying to remove everything that didn't seem
necessary. The code is basically:

int main (int argc, const * char argv[]) {

string toExit;
Thread* t;

while (toExit != "exit") {

cin >> toExit;

if (toExit = "U") {
t = new U();
}
else {
t = new V();
}

t->Start();

delete t;

}

}

Constructors are:

Thread::Thread( ) {}
U::U() {}
V::V() {}

Destructors are:

virtual ~Thread() {}
virtual ~U() {}
virtual ~V() {}

I realize that the main thread s pulling the rug out from under the
pthreads that are running inside the Thread objects... I'm actually
storing the pointers somewhere and joining and deleting threads elsewhere.

Luke


Sigh...

I think I solved my own problem. I really was deleting the Thread object
while it's internal thread was still running. This was causing the "pure
virtual function call" because "this" (passed as a void*) had actually
been deleted, so casting it to a Thread didn't cast it to a U or a V.

If I'm more careful about the delete call, ie joining first or
something, it works fine. Multithreading gets me again...

Sorry to bother everyone.

Luke
Sep 18 '05 #6

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

Similar topics

3
6570
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? Here's what I'm generally trying to achieve: I'm building (trying to anyway) a serialization library. Here's my design:
6
6015
by: Jordi Vilar | last post by:
Hi All, Is there a way to dynamic_cast a pointer to a type defined by a type_info*? something like: type_info *info_of_type_to_cast = typeid(type_to_cast); type_to_cast *casted = really_dynamic_cast<info_of_type_to_cast>(my_data_ptr);
6
1579
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has a public member function called get_value() that is not present in the class OptionBase. My problem is that when calling get_value() I have to cast to Option<some_type>* and I was wondering if I can somehow remove the cast so I dont have to...
5
3414
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
18
3110
by: mark | last post by:
class SORef {...}; class SelectedFORef : public SORef {...}; SOFORef SOCastToSOF(SORef sor) { SelectedFORef sof=nil; sof=dynamic_cast<SelectedFORef>(sor); return sof; };
7
3805
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
6
1720
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in final version*/ inline cell<t>& set_value(t v){ curval = v; return *this;} inline t get_value(){ return curval;} /*actual public interface*/
9
2049
by: jason.cipriani | last post by:
All right, I'm in this weird situation that's hard to explain but I've put together a small example program that represents it. Please bear with the fact that some of the stuff in the example seems useless, it's from a much more complex situation. The example program is confusing for me to think about and to look at, and even though I'm -pretty- sure it's safe, I just want to make sure. The thing in question is casting from a class...
2
1574
by: Lenin | last post by:
Hi, I have a multiply inherited class "C" as folloows. None of them are inherited as virtual base class except that they have virtual functions. class b1 { .... virtual void f1() {} }
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.