Protecting object data from member functions

From: Peter Neal (no_at_email.specified)
Date: 12/10/03


Date: Wed, 10 Dec 2003 16:31:58 -0000

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.



Relevant Pages

  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • Re: Difficulty with nested template classes (newbie)
    ... the huge post but I am including first the errors, then the template ... Reference initialized with 'const double', ... matrix& lookup (int row, int col); ... // iterator operator++ ...
    (comp.lang.cpp)
  • Re: accessor member functions and const
    ... > reference the returned value v. ... int& val ... const int& valconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • [2.6.6-BK] NTFS 2.1.11 Really final cleanups.
    ... throughout the NTFS code to ntfschar since uchar_t is already defined by POSIX ... +BOOL find_attr(const ATTR_TYPES type, const ntfschar *name, const u32 name_len, ... register int rc; ...
    (Linux-Kernel)
  • Simple OOM kill protection interface
    ... Protecting a process does not protect all existing children - only future children ... static int set_oom_protection_state ... pos = snprintf(buffer, len, ... int pid, err, protect; ...
    (Linux-Kernel)

Loading