I want to know if it's somehow possible to in a sub class do a specification of a template function within it's baseclass, or somehow cheat it so that i can have specifications in both.
Printable View
I want to know if it's somehow possible to in a sub class do a specification of a template function within it's baseclass, or somehow cheat it so that i can have specifications in both.
Did I get this right?
You have a template base class and you derived another class from it. You now want to make a specification of a member function of the base class in the derived class. Is this it?
Nope I have a template function within a base class, and I want to do a specification of it in the derived class. (I wonder how you specificate a non template function :D)
I think we mean the same...
What have you tried? My compiler isn't working at the mo, but I'll try to come up with ideas you can try. Just show me the code.
Hmm, i'll think i'll leave it for now, MSVC6 compiler is such an ******* anyway, i get internal compiler errors regularily now :/
If i ever get it work again i'll post the code.
Thanks.
Virtual Functions?
eh?
What exactly do you mean by specification? Are you saying you
want to redefine the function in the derived class?
um not really, a template function will instantiate for the derived class in the base but is to be specified in the base class, something like:
class a{
template <class A>func(A pa){};
};
class b: public a{
template <>func<int>(int pa){};
};
A solution would be to put this into the derived,
template <class A>func(A pa){a::func(pa);};
but thats not the point, I have loads of them inside inside a range of classes and i want to have a common base class to derive them from but still be able to do specifications in the derived classes.
I think you need to make the function virtual in you base class.
Virtual functions allow the derived class to provide an alternative
implementation of a base class function.
Ex:
Code:#include <iostream>
#include <string>
using namespace std;
class Shape{
protected:
string Name;
public:
Shape(){ }
virtual int Perimeter(void)
{
return 0;
}
};
class Rectangle : public Shape {
protected:
int width, height;
public:
Rectangle() : width(0), height(0) { }
Rectangle(int a, int b) : width(a), height(b) { }
int Perimeter(void)
{
return 2*width + 2*height;
}
};
class Square : public Shape {
protected:
int side;
public:
Square() : side(0) { }
Square(int a) : side(a) { }
int Perimeter(void)
{
return 4*side;
}
};
void main(void) {
Shape *ptr;
ptr = new Rectangle(2,4);
cout << ptr->Perimeter() << endl;
ptr = new Square(5);
cout << ptr->Perimeter() << endl;
delete ptr;
}
No, I want to avoid virtual functions at all costs, because they imply overheads and breaks my inlining policy. Besides I won't have any base pointers around, the inheritance i want to do is only for readability purposes
I forgot to use my big word of the day - POLYMORPHISM. :D
hehe, POLYMORPHEUS :D just another dream that i wanted to change
have you tried
?Code:class a{
template <class A>func(A pa){};
};
class b: public a{
template <>a::func<int>(int pa){};
};
yep, it didn't work