CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2003
    Posts
    938

    Inheritence with teamples and member classes

    Hey.

    I ran into a problem recently, where code that i wrote on MSVC doesn't compile on GNU. Here is a sample:

    Code:
    #include <iostream.h>
    
    template <typename T>
    class Base
    {
    protected:
        class Test
        {
        public:
            T entry;
        };
    };
    
    template <typename T>
    class Child: public Base<T>
    {
    public:
        void add();
    };
    
    template <typename T>
    void Child<T>::add()
    {
        Base<T>::Test* test = new Base<T>::Test();
    
    }
    
    int main()
    {
        Child<int> child;
        child.add();
        return 0;
    };
    This code gives me the following error on GNU. Now to fix it i need to do this:

    Code:
    typename Base<T>::Test* test = new typename Base<T>::Test();
    Now this is an annoying thing to have to write every single time i use it. Same with teh Base<T>:: which was not required on MSVC.

    Sorry about a post about GNU, but i figured that differences in c++ compilers would be known around here. I like the MS way of doing it, saves a bunch of code that just complicates everything. Is there a way to get similar behavior on GNU compilers?

    Thanks in advance,
    Quell

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Inheritence with teamples and member classes

    1. You should know by now that iostream.h is completley non-standard. Also note that it's useless in your snippet's context; code/information that's irrelevant to the problem shouldn't be posted.
    2. Free-standing functions don't end in semicolons.
    3. Instead of being lazy and trying to find compiler extensions that suit you, write conformant code and use a simple typedef:
      Code:
      template <typename T>
      class Child: public Base<T>
      {
      public:
          void add();
          typedef typename Base<T>::Test TestType; //Or whatever access is appropriate...
      };
      
      //...
      
      template <typename T>
      void Child<T>::add()
      {
          TestType* test = new TestType(); //Smart pointers are your friend(s).
      }
    4. You should look into smart pointers.

  3. #3
    Join Date
    Aug 2003
    Posts
    938

    Re: Inheritence with teamples and member classes

    Thx alot for the response. Yeah i know the code is dirty as hell, but i guess typedefs are the only proper way to go here.

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