What c++ doesn't show error

F

Faisal

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

Leandro Melo

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

Faisal

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

Leandro Melo

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


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

Leandro Melo

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.
 
J

Juha Nieminen

Faisal said:
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?
}
 
G

gw7rib

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

James Kanze

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.
 
G

gw7rib

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top