Thread: template and polymorphism

  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
    28,413
    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>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(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, 09: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, 01:29 AM
  5. Replies: 3
    Last Post: 10-31-2005, 12:05 PM