Protecting object data from member functions

P

Peter Neal

Hi,

Consider the following (contrived!) class:

class myclass
{
public:
void display() const;
void double_X(); // x = x^2
void double_Y(); // y = y^2

private:
int X;
int Y;
};

I can protect X and Y from mutation in the display function by defining it
as const.

How would I protect Y from mutation whilst inside the double_X function and
likewise X whilst inside double_Y?

Any help gratefully received!

Pete.
 
K

Karl Heinz Buchegger

Peter said:
Hi,

Consider the following (contrived!) class:

class myclass
{
public:
void display() const;
void double_X(); // x = x^2
void double_Y(); // y = y^2

private:
int X;
int Y;
};

I can protect X and Y from mutation in the display function by defining it
as const.

How would I protect Y from mutation whilst inside the double_X function and
likewise X whilst inside double_Y?

You can't.
In this case it is an all or nothing situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top