struct X
{
void foo () {}
void const_foo () const {}
};
struct Y
{
X x;
int i;
X* operator-> () { return &x; }
const X* operator-> () const { return &x; }
};
int main ()
{
Y y;
y->foo(); // invokes `Y::operator->`
y->const_foo(); // invokes `Y::operator->`; BUT I want `Y::operator-> const`!!
}
As demo-ed in the code, I want the const
version of operator->
to be invoked if it's used for invoking a const_foo()
method.
My intention is to automate this. Hence below is NOT a solution, I am looking for:
const_cast<const Y&>(y)->const_foo(); // Don't want this explicit casting
Is there any way to achieve this?
Using template
s or changing the body of X, Y
or changing the declaration of their object (e.g. Y y
) is fine. Just, that I don't want to use the explicit casting in the code at the places of the method invocations.
Purpose: Above is a very simplistic example. If the operator->() const
is selected, then I am calling some extra code/methods within it. For any const
methods of X
being called such as X::const_foo()
, it's desirable to call Y::operator->() const
for this extra code to be invoked.