template instanciation

Dear all,
I have a probkelem with a template function. I have 2 classes: Base and SpecializationClass.
I also have a template function of which i made one instanciation (for SpecializationClass as a template parameter)
I alsop have a pointer of type Base*, it points to a SpecializationClass.

Is there any way to make it use the template function of SpecializationClass when calling it with Base* ?
The way it behaves now it does not compile, because "Base" does not have the attribute "attribute" it needs to compile "Function".
I hope I explained the problem...
greetings
LJS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

// Base class.
class Base
{
public:
    int stuff;
};


// specialization class
class SpecializationClass: public Base
{
public:
    int attribute;
};


// definition of a template function 
template <class T> T Function(T* argument)
{
    cout << argument->attribute;
}


// instanciation of template function for derived class
template SpecializationClass Function(SpecializationClass* argument);


int main()
{

    Base* myObject = new SpecializationClass();

    Function(myObject); // here I want it to call "Function" with a SpecializationClass type...
                        // but it calls it with a Base* object.

    return 0;
}
Last edited on
Function(static_cast<SpecializationClass*>(myObject));, perhaps.

There is no way for the type to be promoted from Base* to SpecializationClass* without an explicit cast.
that is a pity, because I wanted to make more specialization classes and I had hoped it would choose the right function for each one. But thanks!
Topic archived. No new replies allowed.