template derivation from another class?

This is a discussion on template derivation from another class? within the C++ Programming forums, part of the General Programming Boards category; I could use some help with the following concept: suppose i have a class that contains virtuals Code: class b ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    42

    template derivation from another class?

    I could use some help with the following concept:
    suppose i have a class that contains virtuals

    Code:
    class b {
      public:
            b(int n) : _num(n) {};
            virutal int do() = 0;
      private:
       int _num;
    };
    
    
    tempate <class  c >
    class a {
    
          a ( int r)  {
                 c_ = new c(r);
                 c_.do();
          }
    }
    
    class d: class b {
             public: 
                 d(int n): b(n)  { } ;
    }
    
    
    //usage example 
    main () {
         a<d>    var( 5 ) ;
    }
    I would like to enforce that the template class c type be inherited from b... How can this be done using templates ?

    thanks
    Ken

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    If so, then why make a a class template? You might as well use runtime polymorphism by using a pointer/reference to b.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by bean66 View Post
    I would like to enforce that the template class c type be inherited from b... How can this be done using templates ?
    I agree with Laserlight... But you could take advantage of type slicing to ensure that c derives from b. If you assign (by value) a "c" to a "b" it will result in a compiler error if "c" is not a subclass of "b".
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    I did that originally using runtime polymorphism. But since i create and store an array of elements of type "b" in class "a" it required a friend function/helper and an initializer template to create the class "d" derived types that are stored in "a".

    So my thought was just to provide the class as a template parameter. That way the constructor for class "a" could construct the class "c" items...

    I hope that makes sense...

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    Quote Originally Posted by brewbuck View Post
    I agree with Laserlight... But you could take advantage of type slicing to ensure that c derives from b. If you assign (by value) a "c" to a "b" it will result in a compiler error if "c" is not a subclass of "b".
    so something like this:

    Code:
     template <class c> 
      class a {
        public:
                a(int num) {
                      _b = new c(num);
                }
        private: 
           class  b  *_b;
      };

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    I suppose that you want to derive "c" from "b" only to impose an implementation of the function "do". If so, then the new standard will probably offer you the concept of a "concept", with which you can impose functionality of template parameters.

    Code:
    concept B {
      int do_();
    };
    
    tempate <B c>
    class A {
    ..
    };
    The subject is extremely interesting, here is an interesting paper on the subject :
    http://www.research.att.com/~bs/popl06.pdf

    and here is an un-understandable paper, just for completeness :
    http://www.open-std.org/Jtc1/sc22/wg...2005/n1886.pdf

  7. #7
    tjb
    tjb is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    The concepts make compiler errors friendlier, but any call to a nonexistent method will cause the compile to fail. I suppose one way of creating a better message (with pre c++0x compilers) could be to create a method in b with a name that implies an error:

    Code:
    class b
    {
         public:
         b(int n) ...
         virtual int do_() = 0;
         void template_param_not_derived_from_b_error() { }
    };
    
    template <class c>
    class a
    {
         public:
              a(int n) {
                    b_ = new c(n);
                    b_->template_param_not_derived_from_b_error();
                    b_->do_();
              }
         .....
    };

    I assume that this is the main goal...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 06:50 PM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 10:42 PM
  3. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  4. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21