Inheriting a template class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shakje
    New Member
    • Jan 2007
    • 5

    Inheriting a template class

    Ok, quick question. I want to create a base class really just to use in a polymorphic vector, but since there will be generic stuff in there I want there to be a bit of functionality. Now, there's going to be a pointer in there, but depending on the inherited class, the type of pointer could change. I was wondering if it was possible to make the base class a template class, and specify the template when I inherit. If that makes no sense, see below. I would just code it out, but I'm in the middle of planning some other stuff and I don't want to get into coding it if it won't even work.

    Does the below make sense?

    Code:
    template <class T>
    class CBase
    {
        T* m_pointer;
        CBase();
        ~CBase();
    }
    
    class CProcessInfo : CBase<CProcess>
    {
        etc...
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think that should work.

    Comment

    • Shakje
      New Member
      • Jan 2007
      • 5

      #3
      Just to clarify, the code has to be extra readable, I guess I could just use a void* and overload it as necessary I just thought this might be a bit more elegant if it works. Another related question, if I have say:

      Code:
      enum TYPE {  PT_NULL,
                            PT_PROCESS,
                            PT_VARIABLE,
                            etc };
      
      static const TYPE m_type = PT_NULL;
      When I inherit the class can I redefine m_type?

      As you can guess my inheritance logic is a bit shady, I've been doing Java in a course for the last year and just getting back into my C++ :)

      Comment

      • Shakje
        New Member
        • Jan 2007
        • 5

        #4
        Never mind, just being silly, if I initialise it in the constructor it should all be fine.

        If it comes down to it, I'll just have it as a private static with a getter only.

        Thanks for the reply banfa :)

        Comment

        Working...