Click to See Complete Forum and Search --> : Need an architecture


Kohinoor24
May 27th, 2002, 02:31 AM
I Have 2 classes "Writerthread" and "readerThread".
I have 2 other classes "WinThread" & "LinuxThread" which are base classes for the Writerthread" and "readerThread".

I want to derive these classes say,

class WriterThread:public WinThread or
class WriterThread:public LinuxThread

The problem is I want to do it dynamically.The writerthread should derive from 1 of these classes,depending on the operating system(ie:wheather it is linux or windows).In the main(), Iam using a
#ifdef _LINUX_
#endif
#ifdef _WIN_VER
#endif
I need an architecture so that I can derive from 1 of these classes dynamically.
I dont want to use #ifdef inside the classes

Thanks

j0nas
May 27th, 2002, 05:52 PM
:confused:

Linux and Windows are two different operating systems:p... I don't think you can do this check in run-time--don't you need to compile your program for these two OS?

Maybe I just don't get it:confused: :confused: :confused:

jfaust
May 27th, 2002, 10:03 PM
You can dynamically change a class's base class by using a template class and deriving from the template parameter. It would look something like this, although I didn't try to compile it.

template<class T>
class WriterThread : public T
{
};

then in main:

#ifdef _LINUX_
writer = new WriterThread<LinuxThread>();
#else
writer = new WriterThread<WinThread>();
#endif

Jeff

Elrond
May 28th, 2002, 03:57 AM
Why not something like
#ifdef _LINUX_
class WriterThread : public LinuxThread
#endif
#ifdef _WIN_VER
class WriterThread : public WinThread
#endif

If everything is well defined after that, you may be able not to use these defines in the rest of your class.