Language: CPP (C++)
I’m not sure if Dreamed this up, but is it possible to pass a class object as a parameter to a function that expects NOT the class type, but a type that is IN the class.
It would be really nice to, instead of doing function(object->target_variable)
that I could have some rule, so i could then just do
function(object)
which would equal:
function(object->target_variable)
Is there some way to specify this desired behaviour?
You can create a cast operator in the class, then every time some function expects a certain type and you provide an instance of the class it will provide its member. Doesn’t work if you have multiple members you might want to be handled like this (with the same type).
Or you can overload the function, accepting a reference to an instance of the class and calling the original function with the instances member instead. The drawback here is that you need to overload the function for every class, or create a common ancestor for all these classes (an interface or “contract”).