template and polymorphism

This is a discussion on template and polymorphism within the C++ Programming forums, part of the General Programming Boards category; Hi, I wonder if I can use template with polymorphism. The problem is that it seems impossible to define a ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    template and polymorphism

    Hi,
    I wonder if I can use template with polymorphism. The problem is that it seems impossible to define a pointer to an uninstantiated template. For instance, here is a toy example,
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    template<class T>
    class A {
     public:
     T a;
     virtual void f(){ cout<< "A"<< endl;}
    };
    
    template<class T>
    class B: public A<T> {
     public:
     void f(){ cout<< "B"<< endl;}
    };
    
    template<class T>
    class C: public A<T> {
     public:
     void f(){ cout<< "C"<< endl;}
    };
    
    int main(int argc, char **argv){
    A * ptr; // error: missing template arguments before ‘*’ token
    if (strcmp(const_cast<const char*>(argv[1]), "B") == 0){
     if  (strcmp(const_cast<const char*>(argv[2]), "int") == 0) ptr = new B<int>;
     else  if(strcmp(const_cast<const char*>(argv[2]), "float") == 0) ptr = new B<float>;
    } else if(strcmp(const_cast<const char*>(argv[1]), "C") == 0){
     if  (strcmp(const_cast<const char*>(argv[2]), "int") == 0) ptr = new C<int>;
     else  if (strcmp(const_cast<const char*>(argv[2]), "float") == 0) ptr = new C<float>;
    }
    ptr->f();
    return 0;
    }
    Is there any possible way to use template and polymorphism?
    THanks and regards!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by lehe
    I wonder if I can use template with polymorphism.
    Yes, you can.

    Quote Originally Posted by lehe
    The problem is that it seems impossible to define a pointer to an uninstantiated template.
    Yes, but actually I think that the problem is deeper than that: a B<T> is-a A<T>, so a B<int> is-a A<int>, but a B<float> has nothing to do with an A<int>, and likewise for a C<float>.
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Even if you inherit, the T's need to be the same right? So could you do something like:
    Code:
    A<int>* ptr;
    (obviously I didn't try).
    But I don't know of a way to be able to have one thing that could be either an A<int> or an A<float> or whatever, if that's what you're asking. (Someone will probably come along with the way where possible. I don't expect it to be easy.)

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    If use
    Code:
    A<int>* ptr;
    g++ will complain at later code line:
    error: cannot convert B<float>* to A<int>* in assignment
    Similarly for
    Code:
    A<float>* ptr;
    Really appreciate if someone can come up with a solution.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by lehe
    Really appreciate if someone can come up with a solution.
    What is the problem that you are trying to solve?
    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

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    I want to see if it is possible to achieve something like defining a single "pointer" pointing to a "base class" without type argument specified i.e. uninstantiated template base class, so code can be shared not only within polymorphism of each instantiation but also across various type argument. Just like the way we use virtual function.

    A new question is that if the type argument can be determined dynamically? something like this
    Code:
    A <argv[1]> a;
    I know it is wrong the type keyword is not string. How to do it if possible?

    Thanks!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by lehe
    I want to see if it is possible to achieve something like defining a single "pointer" pointing to a "base class" without type argument specified i.e. uninstantiated template base class, so code can be shared not only within polymorphism of each instantiation but also across various type argument.
    The template argument must be specified in the syntax of the pointer declaration.

    Quote Originally Posted by lehe
    A new question is that if the type argument can be determined dynamically?
    Dynamically at run time, no. The template argument must be known at compile time.
    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

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Just define a non-template base class:

    Code:
    struct A
    {
    	virtual void f( void )
    	{
    		cout << "A::f( )" << endl;
    	}
    };
    
    template < typename Type >
    struct B : A
    {
    	virtual void f( void )
    	{
    		cout << "B::f( )" << endl;
    	}
    };
    
    int main( void )
    {
    	B< float >
    		bf;
    	B< int >
    		bi;		
    	A*
    		a = 0;
    	a = &bf;
    	a->f( );
    	a = &bi;
    	a->f( );
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mixed template and virtual polymorphism
    By m37h0d in forum C++ Programming
    Replies: 0
    Last Post: 06-09-2009, 07:15 AM
  2. Passing Functor as template parameter
    By MarkZWEERS in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2008, 08:13 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. true virtual templates
    By Sebastiani in forum C++ Programming
    Replies: 11
    Last Post: 02-27-2006, 12:29 AM
  5. Replies: 3
    Last Post: 10-31-2005, 11:05 AM

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