Re: private member access



"Peter Carlson" <peter@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:OqP8rHUIGHA.3100@xxxxxxxxxxxxxxxxxxxx
> I know this is a very simple c++ question, but I thought that private
> data members were only accessible inside the class. So why is A.x
> accessible to the function IsA?
>
> class A {
> public:
> bool IsA(A &a) {
> return (a.x == x) ? true : false;
> }
> private:
> int x;
> };
>
>
> Peter

Because access rights are class-wide, not object-wide. Thus if you have

A a1, a2;

then a1 has access rights to a2 and vice versa.


--
John Carson


.



Relevant Pages

  • Re: subclassing question
    ... You confuse a visibility of a method ... with its access rights. ... Argument overloading matching. ... Every name in the base class (including private ones) is visible ...
    (microsoft.public.vc.language)
  • Re: by default, public or private?
    ... A value delclared inside a method can only be private to that method. ... value on Class level can have other access rights. ... from Randy). ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Confirm this please - Access Specifiers
    ... I went for a interview recently for a C++ role. ... to the data members of that class would be 'private'. ... Default access for class data members is private, ... With a C++ struct, by ...
    (microsoft.public.vc.language)
  • shared data between classes
    ... Trying to get a feel for how to access private data members without ... void Increment; ... void CommonStuff::Increment (unsigned int which) ...
    (comp.object)
  • Re: Hiding implementation of types in classes?
    ... "Dmitry Epstein" wrote in message ... > private section in a class. ... As a matter of style, ... Data members always declared at the end. ...
    (comp.lang.cpp)