I'm trying to write a template class that overrides some functions of a given class. A simple example would be best to describe what I'm trying to do...
#include <iostream>
using namespace std;
class box{
public:
void print(){ cout << "Good!"; }
};
template <class T>
class shadow{
T obj;
public:
T& operator. (void) { return obj; }
};
int main(void){
shadow<box> t;
t.print();
}
Check in main what I want to do. I want to simulate the call to one of box' methods through the template class. It seems operator '.' can't be overloaded. The closest I've managed to get to this, is by overloading the operator ().
T& operator() (void) { return obj; }
and calling t().print() in main. Could you propose a better way to do what I'm trying to? Thanks in advance.
ps: I really have to do it this way, I don't want to use inheritance.
operator .cannot be overloaded, but you can overloadoperator *, then callt->print().operator*ofshadowand use thet->print()syntax like smart pointers do.(*t).print().operator->is needed fort->print(), like in your answer. Thanks for the correction.