Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What c++ doesn't show error

Reply
Thread Tools

What c++ doesn't show error

 
 
Faisal
Guest
Posts: n/a
 
      11-12-2008
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?
 
Reply With Quote
 
 
 
 
Leandro Melo
Guest
Posts: n/a
 
      11-12-2008
On 12 nov, 09:08, Faisal <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Faisal
Guest
Posts: n/a
 
      11-12-2008
On Nov 12, 4:21*pm, Leandro Melo <(E-Mail Removed)> wrote:
> On 12 nov, 09:08, Faisal <(E-Mail Removed)> 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?
 
Reply With Quote
 
Leandro Melo
Guest
Posts: n/a
 
      11-12-2008
On 12 nov, 09:21, Leandro Melo <(E-Mail Removed)> wrote:
> On 12 nov, 09:08, Faisal <(E-Mail Removed)> 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


 
Reply With Quote
 
Leandro Melo
Guest
Posts: n/a
 
      11-12-2008
On 12 nov, 09:29, Faisal <(E-Mail Removed)> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-12-2008
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?
}
 
Reply With Quote
 
gw7rib@aol.com
Guest
Posts: n/a
 
      11-12-2008
On 12 Nov, 17:37, Juha Nieminen <(E-Mail Removed)> 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?
}
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-13-2008
On Nov 12, 11:38 pm, (E-Mail Removed) wrote:
> On 12 Nov, 17:37, Juha Nieminen <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
gw7rib@aol.com
Guest
Posts: n/a
 
      11-13-2008
On 13 Nov, 09:03, James Kanze <(E-Mail Removed)> wrote:
> On Nov 12, 11:38 pm, (E-Mail Removed) wrote:
> > On 12 Nov, 17:37, Juha Nieminen <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: When running Ant on command line, how to not-show its WARNING messages and only show ERROR message? Roedy Green Java 5 12-13-2011 05:49 PM
Re: When running Ant on command line, how to not-show its WARNINGmessages and only show ERROR message? Lew Java 0 12-10-2011 01:46 AM
Re: When running Ant on command line, how to not-show its WARNING messages and only show ERROR message? John B. Matthews Java 0 12-09-2011 05:19 PM
TO show or NOT to show Matt ASP .Net 1 05-02-2005 09:07 PM
Command "show run" does not show all interfaces Peter Jonas Cisco 3 01-05-2004 12:13 AM



Advertisments