ebony.soft
11/12/2008 11:50:00 AM
On Nov 12, 2:29 pm, Faisal <faisal...@gmail.com> wrote:
> On Nov 12, 4:21 pm, Leandro Melo <ltcm...@gmail.com> wrote:
>
>
>
>
>
> > On 12 nov, 09:08, Faisal <faisal...@gmail.com> wrote:
>
> > > Hi,
>
> > > I've a class
>
> > > class A
> > > {
> > > private:
> > > int _mem;
> > > public:
> > > void foo(A a)
> > > {
> > > _mem = 0;
> > > _a.mem = 0;//not showing access viloation error
> > > }
>
> > > };
>
> > > In the above function I'm accessing object a's private member by
> > > a._mem. Why c++ is not restricting private member access inside a
> > > member fn
> > > even if the object is different?
>
> > > Is there any specific reason for this?
>
> > Because foo is a member function of A. Inside member functions you're
> > allowed to access private members on any instance of the class (not
> > only on the 'this' instance).
>
> > --
> > Leandro T. C. Melo
>
> I would like to know why c++ allows it. Is there any particular reason
> for this?- Hide quoted text -
>
> - Show quoted text -
The life will be difficult if you can't access to yourself. If A
member function of class A doesn't has access to its object, you as a
class designer will have to
1. declare data members public or
2. define accessor member functions for all data members.
Best
Saeed Amrollahi