late binding with template function

This is a discussion on late binding with template function within the C++ Programming forums, part of the General Programming Boards category; hmm stuck with this problem: i'd like to have a template function in an interface, for e.g.: Code: class interface ...

  1. #1
    clf
    clf is offline
    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, 04:45 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 08:50 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

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