An employee asked about some code like this that originally had templates.
I deleted the templates, but the main question remains: why compile it OK?
#include <iostream>
class X
{
public:
void foo() { std::cout << "Here\n"; }
};
typedef void (X::*XFUNC)() ;
class CX
{
public:
explicit CX(X& t, XFUNC xF) : object(t), F(xF) {}
void execute() const { (object.*F)(); }
private:
X& object;
XFUNC F;
};
int main(int argc, char* argv[])
{
X x;
const CX cx(x,&X::foo);
cx.execute();
return 0;
}
Given that CX is a const object and its member function executes const, therefore, inside CX :: execute, this const pointer.
But I can call the non-const member function through a member function pointer.
Are member functions a pointer to a documented hole in the const -ness of the world?
What (supposedly obvious to others) problems did we miss?
source
share