CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Need an architecture

    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 WriterThreadublic WinThread or
    class WriterThreadublic 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

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664


    Linux and Windows are two different operating systems... 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

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  4. #4
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    Re: Need an architecture

    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.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured