Thread: late binding with template function

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    late binding with template function

    hmm stuck with this problem:

    i'd like to have a template function in an interface, for e.g.:
    Code:
    class interface {
    	virtual void somefun(int)=0;
    	
    	template<typename T>
    	/*virtual not possible*/ void anotherfun(T)/*=0*/;
    }
    
    class SomeObject : public interface{
    	void somefun(int i) {
    		...
    	}
    	
    	template<typename T>
    	void anotherfun(T t) {
    		...
    	}
    }
    
    void doSomething(Interface &i) {
    	i.somefun(30);
    	/*following line gives linker error*/
    	i.anotherfun(23);
    }
    
    int main() {
    	SomeObject so;
    	doSomething( so );
    }
    is it impossible to create a virtual template function ? (why?)
    is there another solution to this problem ?

    thanks
    cla

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >is it impossible to create a virtual template function ? (why?)

    Yes. It'd be hard for the compiler/linker to keep track of all the possible virtual functions.

    >is there another solution to this problem ?

    You could try templatising your class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM