[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

What c++ doesn't show error

Faisal

11/12/2008 11:09:00 AM

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?
12 Answers

Leandro Melo

11/12/2008 11:22:00 AM

0

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

Faisal

11/12/2008 11:30:00 AM

0

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?

Leandro Melo

11/12/2008 11:31:00 AM

0

On 12 nov, 09:21, 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


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


--
Leandro T. C. Melo


ebony.soft

11/12/2008 11:39:00 AM

0

On Nov 12, 2:08 pm, 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?

Hi

Because a member function of class A has access to private members of
an object of itself. Compare the foo member function with other
following Foo s:
class B {
int i;
};
class A {
public:
void foo(B b)
{
b.i = 1; // cannot access private member declared in class B
}

};

or

class A {
int _mem;
};

void foo(A a) // non-lobal function
{
a._mem = 0; // cannot access private member declared in class A
}

In other words a member function of class A is friends to all objects
of A.

Regards,
Saeed Amrollahi

ebony.soft

11/12/2008 11:50:00 AM

0

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

Leandro Melo

11/12/2008 11:57:00 AM

0

On 12 nov, 09:29, Faisal <faisal...@gmail.com> 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

Pete Becker

11/12/2008 12:02:00 PM

0

On 2008-11-12 06:29:41 -0500, Faisal <faisalm83@gmail.com> said:

> On Nov 12, 4:21 pm, Leandro Melo <ltcm...@gmail.com> wrote:
>> On 12 nov, 09:08, Faisal <faisal...@gmail.com> wrote:
>>
>>
>>> I've a class
>>
>>> class A
>>> {
>>> private:
>>>         int _mem;
>>> public:
>>>         void foo(A a)
>>>         {
>>>                 _mem = 0;
>>>                 _a.mem = 0;//not showing access viloa
> tion 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).
>>
>
> I would like to know why c++ allows it. Is there any particular reason
> for this?

Try writing a copy constructor or a copy assignment operator when you
can't get at the internals of the thing you're copying.

Access restrictions in C++ help protect against errors, not against malice.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Juha Nieminen

11/12/2008 5:38:00 PM

0

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?
}

Jeff Schwab

11/12/2008 6:36:00 PM

0

Faisal wrote:
> On Nov 12, 4:21 pm, Leandro Melo <ltcm...@gmail.com> wrote:

>> Inside member functions you're
>> allowed to access private members on any instance of the class

> I would like to know why c++ allows it. Is there any particular reason
> for this?

The compiler doesn't necessarily know whether the incoming instance is
"this" or not, because the decision may not be made until run time.
Anyway, C++ isn't python; the question usually is whether there's any
particular reason to prohibit something, rather than whether there's any
reason to allow it. C++ gives you a lot of rope.

gw7rib

11/12/2008 10:38:00 PM

0

On 12 Nov, 17:37, Juha Nieminen <nos...@thanks.invalid> 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?
}