dt*****@rijnh.nl (Dave Moore) wrote in message news:<30*************************@posting.google.c om>...
Julie <ju***@nospam.com> wrote in message news:<40***************@nospam.com>... Dave Moore wrote:
"Manuel Maria Diaz Gomez" <Ma*********************@cern.ch> wrote in message news:<c6**********@sunnews.cern.ch>...
> Thanks Victor for your previous answer!
>
> I have the following problem:
>
> I would like to implement an interface class that defines a pure virtual
> method that could take any parameter as input, void inlcuded.
Just overload the method with one that has no arguments ...
For example, (re-writing your sans errors 8*)
template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type
virtual void do_something() =0; // no argument version
};
class empty {};
class foo : public A<empty> {
public:
virtual void do_something() {}
};
Now the above should work fine, and be called through the no-argument
virtual function in the interface.
HTH, Dave Moore
Suppose that the problem requires that there is only 1 do_something method that
exactly and only corresponds to the type T.
Well, that wasn't in the original deal, 8*) but I think it is
reasonable to assume that you will know which version (with or without
argument) you want to override, so you can simply declare the other
one private in the derived class.
class foo2 : public A<int> {
public:
virtual void do_something(int); // override version with argument
private:
void do_something(); // hide version without argument
};
It is not an air-tight solution, but without seeing a more in-depth
example, I can't think of anything else. The TMP wizards might be
able to come up with a more elegant solution I suppose.
Hmmm .. actually, it may not be a solution at all, because the no-arg
version is still public in the base class, so it could be called
through a pointer to the base class at run-time. I'm not sure what
would happen in this case if the object pointed-to was a derived type
with a private version of the function, but I guess it wouldn't be
good. Actually, I don't have much experience with run-time
polymorphism, so maybe I should keep my mouth (er, keyboard?) shut and
let the experts answer these questions. 8*)
OTOH, I *do* learn a lot when I get shot down by those more
knowledgable than me ... I just don't want to screw up the person I
was trying to help in the first place.
Dave Moore