Consider the following code:
struct Foo
{
Foo operator+(const Foo &rhs) const;
// notice lack of: Foo operator*(const Foo &rhs) const;
};
template <class T>
struct Bar
{
T x, y;
T add() const { return x + y; }
T mul() const { return x * y; }
};
I have two questions:
-
Can I inherit from
Bar<Foo>and overridemul()to something meaningful? -
Can I inherit from
Bar<Foo>without overridingmul()if I never usemul()anywhere?
Templates are really a kind of smart preprocessor, they’re not compiled. If you don’t use something, you can write complete (syntactically correct) rubbish, i.e you may inherit from
P.S. since your + operator is used in a const function, it should be declared as const too.
EDIT: OK, not all compilers support this, here’s one that compiles with gcc: