Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem accessing protected member of another object

Reply
Thread Tools

Problem accessing protected member of another object

 
 
gw7rib@aol.com
Guest
Posts: n/a
 
      03-02-2008
I'm getting in knots about accessing a protected member. I was under
the impression that (contrary to what one might expect) access to
members is worked out on a per-class basis rather than a per-object
basis. But I'm still having a problem. The following code illustrates
it:


void use(int val);

class Base {
protected:
int val;
public:
void foo(Base *other);
};

void Base::foo(Base *other) {
use(other -> val); /* Line 1 - works */
}

class Derived : public Base {
public:
void bar(Base *other);
};

void Derived::bar(Base *other) {
use(val); /* Line 2 - works */
use(other -> val); /* Line 3 - doesn't work */
}

At line 1, I am accessing a member of another object, but this is
allowed as it is the same class and it is the class, rather than the
specific object, that matters.

At line 2, I am accessing a protected member of the base class. This
works - it is an example of what "protected" means.

But line 3 does not work, even though I am allowed to access the val
member of the base class of the same object (see line 2) and (see
line 1) the actual object shouldn't matter for acessing purposes. What
is this line doing wrong that the other two aren't?

Thanks for any explanation.
Paul.

 
Reply With Quote
 
 
 
 
Christopher Swiedler
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 12:30 pm, (E-Mail Removed) wrote:
> void use(int val);
>
> class Base {
> protected:
> int val;
> public:
> void foo(Base *other);
> };
>
> void Base::foo(Base *other) {
> use(other -> val); /* Line 1 - works */
>
> }
>
> class Derived : public Base {
> public:
> void bar(Base *other);
> };
>
> void Derived::bar(Base *other) {
> use(val); /* Line 2 - works */
> use(other -> val); /* Line 3 - doesn't work */
>
> }


See http://publib.boulder.ibm.com/infoce...ef/cplr129.htm

I think the problem is that you can access protected members of Base
only when doing so through a pointer or reference to Derived. When you
access val in the current object, you're doing so through an implicit
this, which is a Derived *, so it works. When you access other->val,
it's a Base *, so it doesn't work. You can cast "other" to a Derived
*, change the function signature to take a Derived *, or make Derived
a friend of Base.

Why that is, I'm not quite sure. My expectations would be that the
type of the pointer wouldn't matter.

chris
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-02-2008
* Christopher Swiedler:
> On Mar 2, 12:30 pm, (E-Mail Removed) wrote:
>> void use(int val);
>>
>> class Base {
>> protected:
>> int val;
>> public:
>> void foo(Base *other);
>> };
>>
>> void Base::foo(Base *other) {
>> use(other -> val); /* Line 1 - works */
>>
>> }
>>
>> class Derived : public Base {
>> public:
>> void bar(Base *other);
>> };
>>
>> void Derived::bar(Base *other) {
>> use(val); /* Line 2 - works */
>> use(other -> val); /* Line 3 - doesn't work */
>>
>> }

>
> See http://publib.boulder.ibm.com/infoce...ef/cplr129.htm
>
> I think the problem is that you can access protected members of Base
> only when doing so through a pointer or reference to Derived. When you
> access val in the current object, you're doing so through an implicit
> this, which is a Derived *, so it works. When you access other->val,
> it's a Base *, so it doesn't work. You can cast "other" to a Derived
> *, change the function signature to take a Derived *, or make Derived
> a friend of Base.
>
> Why that is, I'm not quite sure. My expectations would be that the
> type of the pointer wouldn't matter.


If you could then you could access protected members of SomeonesDerived
by deriving from SomeonesBase (that is, the protected members introduced
in that base class).

The bad news is that this protection isn't 100%.

It's only designed to protect against inadvertent access attempts, not
against serious hacking or in general use of low level language features
(note: member pointers are a low level language feature).


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Amal
Guest
Posts: n/a
 
      03-03-2008
On Mar 3, 1:30 am, (E-Mail Removed) wrote:
> I'm getting in knots about accessing a protected member. I was under
> the impression that (contrary to what one might expect) access to
> members is worked out on a per-class basis rather than a per-object
> basis. But I'm still having a problem. The following code illustrates
> it:
>
> void use(int val);
>
> class Base {
> protected:
> int val;
> public:
> void foo(Base *other);
> };
>
> void Base::foo(Base *other) {
> use(other -> val); /* Line 1 - works */
>
> }
>
> class Derived : public Base {
> public:
> void bar(Base *other);
> };
>
> void Derived::bar(Base *other) {
> use(val); /* Line 2 - works */
> use(other -> val); /* Line 3 - doesn't work */
>
> }
>
> At line 1, I am accessing a member of another object, but this is
> allowed as it is the same class and it is the class, rather than the
> specific object, that matters.
>
> At line 2, I am accessing a protected member of the base class. This
> works - it is an example of what "protected" means.
>
> But line 3 does not work, even though I am allowed to access the val
> member of the base class of the same object (see line 2) and (see
> line 1) the actual object shouldn't matter for acessing purposes. What
> is this line doing wrong that the other two aren't?
>
> Thanks for any explanation.
> Paul.


-> void Derived::bar(Base *other) {
-> use(val); /* Line 2 - works */
-> use(other -> val); /* Line 3 - doesn't work */
->
-> }

Here in the member function of derived class Base is considered
like any other class. It doesn't make any difference if class Derived
is derived from Base. You cannot access the protected member by
directly referring from an object. So it is not an artifact rather a
perfect implementation.

But you can access the other->val in case if the bar was
accepting a Derived*. This is how copy constructor works.

And for an example let us derive another class from Base.

class AnotherDerivedublic Base
{
/*.....*/
}
AnotherBase AnotherBaseobj;

Now you can cast AnotherBaseobj down to its Base* and pass it to
the member of Derived::bar. Does it make sense? Its invalid for sure.

To detail see the below program.

class Base
{
protected:
int val;
public:
};

class Derivedublic Base
{
public:
void foo( Base* obj )
{
std::cout<<obj->val; // This line will generate error unconditional
to the way in which class Derived is -derived- from class Base
}
};

class AnotherDerivedublic Base
{
};

int main()
{
Derived Obj1;
AnotherDerived Obj2;
Obj1.foo( &Obj2 );
return 0;
}

I guess this answers why this doesn't work in the way you
expected.

Thanks a lot,
Amal P
 
Reply With Quote
 
Martin York
Guest
Posts: n/a
 
      03-03-2008
On Mar 2, 12:30 pm, (E-Mail Removed) wrote:
> I'm getting in knots about accessing a protected member. I was under
> the impression that (contrary to what one might expect) access to
> members is worked out on a per-class basis rather than a per-object
> basis. But I'm still having a problem. The following code illustrates
> it:
>
> void use(int val);
>
> class Base {
> protected:
> int val;
> public:
> void foo(Base *other);
> };
>
> void Base::foo(Base *other) {
> use(other -> val); /* Line 1 - works */
>
> }
>
> class Derived : public Base {
> public:
> void bar(Base *other);
> };
>
> void Derived::bar(Base *other) {
> use(val); /* Line 2 - works */
> use(other -> val); /* Line 3 - doesn't work */
>
> }
>
> At line 1, I am accessing a member of another object, but this is
> allowed as it is the same class and it is the class, rather than the
> specific object, that matters.
>
> At line 2, I am accessing a protected member of the base class. This
> works - it is an example of what "protected" means.
>
> But line 3 does not work, even though I am allowed to access the val
> member of the base class of the same object (see line 2) and (see
> line 1) the actual object shouldn't matter for acessing purposes. What
> is this line doing wrong that the other two aren't?
>
> Thanks for any explanation.
> Paul.



A class is automatically a friend of itself.
That is why line 1 is working.

Because a class is a friend of itself then any object of class A can
access all members (private and protected) of another object of class
A.

Note friendship is not inherited. So children of A have no rights to
access the member of another object of type A. Though the protected
status allows them to access members of the base class that are apart
of the same object.
 
Reply With Quote
 
Sergey Prokazov
Guest
Posts: n/a
 
      03-04-2008
> A class is automatically a friend of itself.
> That is why line 1 is working.


Basically, you are right.
When you declare something like "class A : public B {}", compiler
copies public and protected sections of A to the beginning of B.
Actually compiler has some other things to do, particularly if base
class is polymorphic, but let's omit them for clarity.


Lets have a look at an example:

class Base{
protected:
int val;
public:
void foo(Base *other);
};


class Derived_1
{
protected:
int val;
public:
void foo(Base *other);
};

class Derived_2 : public Base
{
};

Compiler will generate the same code for Derived_1 and Derived_2
objects. Derived_1-type objects and Base-type objects are not related
each other. The same we can say about Derived_2-type objects.
If you need functionality of the existing class in your brand new
class, you don't need to copy-paste it by yourself, you can use
inheritance and compiler will gladly copy-paste it for you.


 
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
Can't take pointer-to-member of protected member of base class K. Frank C++ 8 03-22-2012 06:50 PM
Why can derived member function not access protected member of a baseclass object? blangela C++ 8 09-26-2008 03:24 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Accessing a protected member of a member of type BaseClass???? Steven T. Hatton C++ 2 08-16-2004 03:11 PM
Is this right? Can't call protected member of base class from derivedclass method, for another object Asfand Yar Qazi C++ 17 09-12-2003 04:44 PM



Advertisments