"Ian Collins" <ian-news@hotmail.comwrote in message
news:5a4qanF2nopieU8@mid.individual.net...
Quote:
Jim Langston wrote:
Quote:
>This should illistrate what I am trying to do:
>>
>template <class T>
>T SomeFunction( T parm )
>{
> return parm;
>}
>>
>template <class T>
>class SomeClass
>{
>public:
> SomeClass( T fp ) {}
>};
>>
>int main()
>{
> int x = 0;
> SomeFunction( x );
> // SomeClass sf( x ); // Won't work
> SomeClass<intsf( x );
>}
>>
>I'm trying to instantize a templated class without having to specify the
>template argument. I can do that for templated functions as shown. When
>trying to compile for classes, however, I'm told that:
>error C2955: 'SomeClass' : use of class template requires template
>argument
>list
>>
>For this simple type of int illistrating the problem, it's no big deal.
>All
>I have to do is add <int>. But what I'm actually trying to do is come up
>with a way to store function pointers in a class which this is the first
>piece of. I'm don't want to have to figure out if it's int (*)( float,
>double, std::string).
>>
>I mean, I can in a fuction pass the name of a function and even call it
>in a
>function (with no parms at this point) and I don't have to type in code
>the
>template argument.
>>
You will have to use a function template to wrap what ever it is you are
doing. Unfortunately, that's the way it is.
Okay, this actually works as far as it goes, although I didn't think it
would:
#include <iostream>
#include <vector>
class Base
{
virtual ~Base() {}
};
template <class T>
class SomeClass: public Base
{
public:
SomeClass( T fp ) {}
public:
T fp;
};
template <class T>
SomeClass<T>* SomeFunction( T parm )
{
return new SomeClass<T>( parm );
}
int Foo( int Parm )
{
std::cout << "In Foo\n";
return Parm;
}
double Bar( )
{
std::cout << "In Bar\n";
return 3.1415926;
}
void FooBar( int a, double b, std::string c )
{
std::cout << "In FooBar\n";
return;
}
int main()
{
std::vector<Base*Functions;
Functions.push_back( SomeFunction( Foo ) );
Functions.push_back( SomeFunction( Bar ) );
Functions.push_back( SomeFunction( FooBar ) );
}
Now I just have to figure out some mechanism to call fp( parms ) in the
derived classes :/ I've been scratching my head on this one.