473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What c++ doesn't show error

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?
Nov 12 '08 #1
12 1623
On 12 nov, 09:08, Faisal <faisal...@gmail.comwrote:
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
Nov 12 '08 #2
On Nov 12, 4:21*pm, Leandro Melo <ltcm...@gmail.comwrote:
On 12 nov, 09:08, Faisal <faisal...@gmail.comwrote:
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?
Nov 12 '08 #3
On 12 nov, 09:21, Leandro Melo <ltcm...@gmail.comwrote:
On 12 nov, 09:08, Faisal <faisal...@gmail.comwrote:
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
Nov 12 '08 #4
On Nov 12, 2:08*pm, Faisal <faisal...@gmail.comwrote:
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
Nov 12 '08 #5
On Nov 12, 2:29*pm, Faisal <faisal...@gmail.comwrote:
On Nov 12, 4:21*pm, Leandro Melo <ltcm...@gmail.comwrote:


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

Nov 12 '08 #6
On 12 nov, 09:29, Faisal <faisal...@gmail.comwrote:
>
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
Nov 12 '08 #7
On 2008-11-12 06:29:41 -0500, Faisal <fa*******@gmail.comsaid:
On Nov 12, 4:21Â*pm, Leandro Melo <ltcm...@gmail.comwrote:
>On 12 nov, 09:08, Faisal <faisal...@gmail.comwrote:

>>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)

Nov 12 '08 #8
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?
}
Nov 12 '08 #9
Faisal wrote:
On Nov 12, 4:21 pm, Leandro Melo <ltcm...@gmail.comwrote:
>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.
Nov 12 '08 #10
On 12 Nov, 17:37, Juha Nieminen <nos...@thanks.invalidwrote:
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 '08 #11
On Nov 12, 11:38 pm, gw7...@aol.com wrote:
On 12 Nov, 17:37, Juha Nieminen <nos...@thanks.invalidwrote:
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:ja*********@gmail.com
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
Nov 13 '08 #12
On 13 Nov, 09:03, James Kanze <james.ka...@gmail.comwrote:
On Nov 12, 11:38 pm, gw7...@aol.com wrote:
On 12 Nov, 17:37, Juha Nieminen <nos...@thanks.invalidwrote:
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 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James Coburn's Grey Helmet Hair | last post by:
When I try to compile my GTK# app, the compiler says: jbailo@linux:~/mono> mcs buttons.cs -r gtk-sharp.dll -r glib-sharp.dll -r -o buttons.exe error CS2001: Source file 'buttons.exe' could not...
3
by: Bupkas | last post by:
using System.Windows.Forms; class MessageBoxHelloWorld { public static void Main() {
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: Jassim Rahma | last post by:
I am getting this error and i Don'r know where is the problem? please help.. An unhandled exception of type 'VistaDB.VistaDBException' occurred in vistadb.provider.dll Additional...
11
by: Shawn Yates | last post by:
I downloaded TimeLines.zip from (http://www.mvps.org/access/reports/rpt0018.htm) and I think I found a bug in it but I can't seem to fix it. When you open this go to the table and enter a new...
12
by: Adrian | last post by:
The code below was taken from an example. All the "noise" in the example was thrown out. This is supposedly according to the bridge pattern. What in the code (which lines) represent the bridge...
8
by: Connectcase | last post by:
Hi there, and greetings from the Netherlands. Trying to switch over from VB to VB.NET and struggling with something that seemed very simple: ============================ Public Class Form1...
22
by: jeremito | last post by:
I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method? Thanks, Jeremy
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.