What c++ doesn't show error

Discussion in 'C++' started by Faisal, Nov 12, 2008.

  1. Faisal

    Faisal Guest

    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?
     
    Faisal, Nov 12, 2008
    #1
    1. Advertising

  2. Faisal

    Leandro Melo Guest

    On 12 nov, 09:08, Faisal <> 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
     
    Leandro Melo, Nov 12, 2008
    #2
    1. Advertising

  3. Faisal

    Faisal Guest

    On Nov 12, 4:21 pm, Leandro Melo <> wrote:
    > On 12 nov, 09:08, Faisal <> 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?
     
    Faisal, Nov 12, 2008
    #3
  4. Faisal

    Leandro Melo Guest

    On 12 nov, 09:21, Leandro Melo <> wrote:
    > On 12 nov, 09:08, Faisal <> 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



    Notice that you have some typos in function foo... (_a.mem should be
    a._mem)


    --
    Leandro T. C. Melo
     
    Leandro Melo, Nov 12, 2008
    #4
  5. Faisal

    Leandro Melo Guest

    On 12 nov, 09:29, Faisal <> wrote:
    >
    > I would like to know why c++ allows it. Is there any particular reason
    > for this?


    Well, I think that's the usual behavior in most programming languages.
    Basically, in a object oriented design encapsulation is provided in
    the class level, not in the object level.


    --
    Leandro T. C. Melo
     
    Leandro Melo, Nov 12, 2008
    #5
  6. Faisal wrote:
    > I would like to know why c++ allows it. Is there any particular reason
    > for this?


    How would you write a copy constructor if you weren't able to access
    the private members of the object you got as the parameter of your
    constructor?

    Besides, consider this:

    void A::foo(A a)
    {
    A* ptr;
    if(someObscureFunction())
    ptr = this;
    else
    ptr = &a;

    ptr->privateMember = 5; // Error or not?
    // How would the compiler know?
    }
     
    Juha Nieminen, Nov 12, 2008
    #6
  7. Faisal

    Guest

    On 12 Nov, 17:37, Juha Nieminen <> wrote:
    > Faisal wrote:
    > > I would like to know why c++ allows it. Is there any particular reason
    > > for this?

    >
    >   How would you write a copy constructor if you weren't able to access
    > the private members of the object you got as the parameter of your
    > constructor?
    >
    >   Besides, consider this:
    >
    > void A::foo(A a)
    > {
    >     A* ptr;
    >     if(someObscureFunction())
    >         ptr = this;
    >     else
    >         ptr = &a;
    >
    >     ptr->privateMember = 5; // Error or not?
    >                             // How would the compiler know?
    > }


    But that example's not totally convincing - what about this?

    int someObscureFunction();

    class base {
    protected: int privateMember; };

    class A : public base {
    public: void foo(base a); };

    void A::foo(base a) {
    base* ptr;
    if(someObscureFunction())
    ptr = this;
    else
    ptr = &a;

    this -> privateMember = 5; // OK
    ptr -> privateMember = 5; // Error or not?
    // How would the compiler know?
    }
     
    , Nov 12, 2008
    #7
  8. Faisal

    James Kanze Guest

    On Nov 12, 11:38 pm, wrote:
    > On 12 Nov, 17:37, Juha Nieminen <> wrote:
    > > Faisal wrote:
    > > > I would like to know why c++ allows it. Is there any
    > > > particular reason for this?


    > > How would you write a copy constructor if you weren't able
    > > to access the private members of the object you got as the
    > > parameter of your constructor?


    > > Besides, consider this:


    > > void A::foo(A a)
    > > {
    > > A* ptr;
    > > if(someObscureFunction())
    > > ptr = this;
    > > else
    > > ptr = &a;


    > > ptr->privateMember = 5; // Error or not?
    > > // How would the compiler know?
    > > }


    > But that example's not totally convincing - what about this?


    > int someObscureFunction();


    > class base {
    > protected: int privateMember; };


    > class A : public base {
    > public: void foo(base a);
    > };


    > void A::foo(base a) {
    > base* ptr;
    > if(someObscureFunction())
    > ptr = this;
    > else
    > ptr = &a;


    > this -> privateMember = 5; // OK
    > ptr -> privateMember = 5; // Error or not?
    > // How would the compiler know?
    >
    > }


    This second example is an error. The rights you acquire by
    being in a member function of A only apply to objects known to
    be of type A. Since *ptr is not known to be of type A, member
    functions of A can only access its public members.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 13, 2008
    #8
  9. Faisal

    Guest

    On 13 Nov, 09:03, James Kanze <> wrote:
    > On Nov 12, 11:38 pm, wrote:
    > > On 12 Nov, 17:37, Juha Nieminen <> wrote:
    > > > Faisal wrote:
    > > > > I would like to know why c++ allows it. Is there any
    > > > > particular reason for this?


    > > > How would you write a copy constructor if you weren't able
    > > > to access the private members of the object you got as the
    > > > parameter of your constructor?
    > > > Besides, consider this:
    > > > void A::foo(A a)
    > > > {
    > > >     A* ptr;
    > > >     if(someObscureFunction())
    > > >         ptr = this;
    > > >     else
    > > >         ptr = &a;
    > > >     ptr->privateMember = 5; // Error or not?
    > > >                             // How would the compiler know?
    > > > }


    > > But that example's not totally convincing - what about this?
    > > int someObscureFunction();
    > > class base {
    > >   protected: int privateMember; };
    > > class A : public base {
    > >   public:  void foo(base a);
    > > };
    > > void A::foo(base a) {
    > > base* ptr;
    > > if(someObscureFunction())
    > >   ptr = this;
    > > else
    > >   ptr = &a;
    > > this -> privateMember = 5; // OK
    > > ptr -> privateMember = 5; // Error or not?
    > >                           // How would the compiler know?
    > > }


    > This second example is an error.  The rights you acquire by
    > being in a member function of A only apply to objects known to
    > be of type A.  Since *ptr is not known to be of type A, member
    > functions of A can only access its public members.


    Ah. I didn't explain what I meant at all clearly. Sorry.

    You are right that in my code, the compiler happily objects to an
    error at the "ptr -> privateMember = 5;" line.

    However, my code is similar to Juha's example, and the language could
    have been set up (it wasn't, but it could have been) so that in Juha's
    code the line "ptr->privateMember = 5;" was equally an error. Instead
    the language was set up so that a class is a friend of itself, and
    "ptr->privateMember = 5;" works whether or not ptr is this or not.

    My point was that the existence of code such as Juha's did not compel
    the language to be set up so that a class was a friend of itself.

    The copy constructor argument is a more compelling argument.

    Regards.
    Paul.
     
    , Nov 13, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page