Is there a way to define a conversion operator for any type of pointer?

advertisements

I have this class:

class fileUnstructuredView {
private:
    void* view;
public:
    operator void*() {
        return view;
    }
};

and it can do this:

void* melon = vldf::fileUnstructuredView();

but it cant do that:

int* bambi = vldf::fileUnstructuredView();
//or
int* bambi = (int*)vldf::fileUnstructuredView();

instead i have to do

int* bambi = (int*)(void*)vldf::fileUnstructuredView();

or create another explicit type conversion operator for int*.

The point is, i want to easily convert the class into various pointer types including all the basic ones and some pod structure types. Is there a way to do that without creating a conversion operator for all of them? The closest thing to what I'm asking that I can think of is the ZeroMemory method that doesn't seem to have any types required for its arguments.


Yes, you can have a conversion function template.

template <class T>
operator T*() {
    return static_cast<T*>(view);
}