How to call a non-const member function inside a const member funciton


class MyClass
{
public:

float x;

public:

MyClass();
void a() const;
void b();
};

MyClass::MyClass()
{
x = 0.f;
}

void MyClass::a() const
{
MyClass* ptr = (MyClass*)this;
ptr->b();
}

void MyClass::b()
{
x++;
}

int main( int argc, char* argv[] )
{
MyClass A;
A.a();
cout << A.x << endl;

return 1;
}

Add a Comment