LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2006, 07:38 AM   #1
qwijibow
LQ Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
casting void* to an abstract class


Hello...

Just For Fun... Im Writing a small Game Engine Based on SDL, and im having loads of trouble trying to write a Thead abstract class.

I'm at work at the moment, and dont have access to my code, but the design is like so....


Code:
class IThread
{
public:
    virtual void Main(void)=0;

    void Start(IThead *This);
};

int ThreadStarter(void *thread)
{
    if(thread!=NULL)
    {
        IThread *Thread = reinterpret_cast<IThread*>(Thread);
        Thread->Main();
    }
}


void IThread::Start(IThread* This)
{
    SDL_CreateThread(ThreadStarter, This);
}

I Then Inherit From The IThread Class to create my Background Thread, The Background Thread Implements the virtual Main() of IThread, and calls Start with the this pointer as a parameter.

My Problem is, The ThreadStarter's Call to main Always attempts to run the Virtual Main, Not The Background threads Main().

Whats Going Wrong ???

I Assume the reinterpret_cast is messing up the vtable ???

But How Else Can i Cast a void pointer to an IThread ???

Can Anyone point out is wrong / how to fix it ?
or surgest a better way of implementing a thread class with SDL ???

Thanks !

Last edited by qwijibow; 12-15-2006 at 07:42 AM.
 
Old 12-15-2006, 11:04 AM   #2
bigqueso
LQ Newbie
 
Registered: Jun 2006
Location: Salt Lake City, UT, USA
Distribution: Ubuntu (laptop,home), Suse (work)
Posts: 8

Rep: Reputation: 0
This code seems to work for me on cygwin and Suse 9.3. One thing you might try is to change the name of Thread to thread. There might be some funniness going on otherwise.

IThread *Thread = reinterpret_cast<IThread*>(Thread);

to

IThread *Thread = reinterpret_cast<IThread*>(thread);


Code:
#include <iostream>

using namespace std;

class IThread
{
public:
  virtual ~IThread() {}
  virtual void Main(void) { cout << "IThread\n"; }
};

class Background: public IThread {
public:
  virtual void Main(void) { cout << "Background\n"; }
};

int ThreadStarter(void *thread)
{
  if(thread!=NULL)
    {
      IThread *Thread = reinterpret_cast<IThread*>(thread);
      Thread->Main();
    }
  return 0;
}


int main()
{

  Background background;

  ThreadStarter((void*)&background);

  return 0;
}
James
 
Old 12-15-2006, 03:55 PM   #3
qwijibow
LQ Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Original Poster
Rep: Reputation: 47
Solved.......
Although im not quite sure why.

I Origonaly had the SDL_CreateThread() in the contructor of IThread.
This apears not to work... Although im not quite sure why....

Maybe Because IThread's Constructor was running Before CBackground's Main() was added to the vtable... ?

Compiler Bug ????

Anyways, thanks.
 
Old 12-23-2006, 08:43 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Unless the original pointer was static_cast to IThread* before casting to void*, you are lucky the code ended up working. Technically void* only has to cast back to the same type it was cast from originally. It isn't really a cast, however; it's just a loophole in C++ allowing you to disregard the type of the pointer to make something compile. If the pointer was to an object derived from IThread, the IThread* value might not be the same as 'this'. Real casting entirely depends on how the compiler sees pointers at the time of the cast; if it sees you are casting A to B it knows (because it decides) what offset in value that cast might have. When casting to/from void*, C-casting, or reinterpret_cast'ing you (or someone else) are making a hole in the program for some convenience or another. Casting from void* does absolutely nothing, meaning if you cast A to void* then to B, you are saying that there is a B located where the A really is, which is not required to be true. Normally if B is the first base class of A this is not a problem, however, which is probably why it worked for you afterall.
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
casting std::string to void * lucky6969b Programming 1 03-17-2006 04:38 AM
c++ Abstract Base Classes exodist Programming 1 06-11-2005 06:05 PM
Abstract Control Model juris Linux - Networking 0 10-26-2004 03:45 AM
interface vs abstract (Java) Mohsen Programming 2 07-24-2004 02:06 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration