[Tuxcpprogramming] Making class functions look like variables??

Mark Phillips mark at austrics.com.au
Mon Jul 9 18:16:48 UTC 2001


Rather than defining a class like so:

class myClass {
public:
  int n;
};

a potentially better way is:

class myClass {
private:
  int nPrivate;
public:
  int n() const {return nPrivate;}
};

It is better because our means of
storing n is separated from the
method of accessing it.  This
has certain advantages, for example
allowing us to change our means of
storage down the track.

However it seems silly to have to
type the brackets "()" every time
we wish to access n.  It would be
nice to be able to write:

myClass x;
.
.
.
int i=x.n;

instead of

int i=x.n();

Is there any way of doing this?

Thanks,

Mark.





More information about the tuxCPProgramming mailing list