Re: Why are member functions allowed to access private data members of a class?
Dwight Army of Champions wrote:
I mean, as it is now you have a set_value() function that sets the
value of one of your data members. But there could potentially be
another member function out there of the same class that also sets
this value (by using the this pointer and the assignment operator).
Shouldn't this be disallowed?
That would be enforcing a single paradigm, which C++ generally doesn't
do. You are allowed to use set-functions, but not required.
The class is protected from external manipulation, but the class
members are part of the class, and supposed to know what they are
doing.
A lot of classes do not have a single set_value member, so it
obviously works well anyway.
Bo Persson
On Jun 27, 4:29 pm, Dwight Army of Champions
<dwightarmyofchampi...@hotmail.com> wrote:
Yes I know they are, and they should be the only member functions
that can access the pivate data members. There should be some
special designation that identifies them as not just member
functions but also special member functions.
On Jun 27, 4:23 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Dwight Army of Champions" <dwightarmyofchampi...@hotmail.com>
wrote in
messagenews:e0c6ff41-4a19-4378-95ce-78a5369429ed@c10g2000yqi.googlegroups.com...
I know that no outside functions can directly acces private data
members of a class, and that all interface should be done
through the get and set accessor/mutator functions, but my
question is: What are class member functions allowed to access
their own private data members? Because of these, data members
can be manipulated and changed without use of the accessors and
mutators, and couldn't such behavior wreak havoc on programs?
Why not force the class member functions to also only use
accessors and mutators? That way, if there was a problem, then
the programmer KNOWS that the issue is with the get and set
functions and not anywhere else.
That makes no sense: accessors and mutators are also class member
functions.
/Leigh- Hide quoted text -
- Show quoted text -