Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Access to protected base member

Reply
Thread Tools

Access to protected base member

 
 
Andy Ward
Guest
Posts: n/a
 
      06-17-2004
Given the following code:
class A
{
protected:
int pro;
};

class B : public A
{
public:
void foo()
{
A * a;
a->pro;

B * b;
b->pro2;
}
protected:
int pro2;
};

The line b->pro2 compiles fine, however the line a->pro does not compile.
Could someone please explain to me why.
Thanks.


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      06-17-2004
On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
<(E-Mail Removed)> wrote,
>The line b->pro2 compiles fine, however the line a->pro does not compile.
>Could someone please explain to me why.


Pointer b is promised to point to a B instance, therefore it is
legitimate for class B members to access protected members.

Pointer a can point to A and other derived classes that are not B
instances, therefore it would be wrong for B to have access.

 
Reply With Quote
 
 
 
 
Andy Ward
Guest
Posts: n/a
 
      06-17-2004
"David Harmon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 17 Jun 2004 12:11:48 +1200 in comp.lang.c++, "Andy Ward"
> <(E-Mail Removed)> wrote,
> >The line b->pro2 compiles fine, however the line a->pro does not compile.
> >Could someone please explain to me why.

>
> Pointer b is promised to point to a B instance, therefore it is
> legitimate for class B members to access protected members.
>
> Pointer a can point to A and other derived classes that are not B
> instances, therefore it would be wrong for B to have access.
>


Of course, thank you.


 
Reply With Quote
 
Hunter Hou
Guest
Posts: n/a
 
      06-18-2004
David Harmon wrote:

>
> Pointer a can point to A and other derived classes that are not B
> instances, therefore it would be wrong for B to have access.
>


class B: public A { ... };

public derivation means that base class( A ) protected members can be
used by members and friends of derived class( B ). How does above not
break this rule?

thanks - hunter
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      06-19-2004
On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou
<(E-Mail Removed)> wrote,
>David Harmon wrote:
>
>>
>> Pointer a can point to A and other derived classes that are not B
>> instances, therefore it would be wrong for B to have access.
>>

>
>class B: public A { ... };
>
>public derivation means that base class( A ) protected members can be
>used by members and friends of derived class( B ). How does above not
>break this rule?


public|protected|private derivation mean that protected members of A can
be accessed from B when the A is actually the base class part of a B
object. You do not get freebie access to mess around with the base
class members of C objects, D objects, E objects or any other class that
is foreign to B just because it also derives from A.

I hope that is more clear; I don't know how else to say it.

 
Reply With Quote
 
Hunter Hou
Guest
Posts: n/a
 
      06-21-2004
David Harmon wrote:
> On Fri, 18 Jun 2004 16:51:22 +0800 in comp.lang.c++, Hunter Hou
> <(E-Mail Removed)> wrote,
>
>>David Harmon wrote:
>>
>>
>>>Pointer a can point to A and other derived classes that are not B
>>>instances, therefore it would be wrong for B to have access.
>>>

>>
>>class B: public A { ... };
>>
>>public derivation means that base class( A ) protected members can be
>>used by members and friends of derived class( B ). How does above not
>>break this rule?

>
>
> public|protected|private derivation mean that protected members of A can
> be accessed from B when the A is actually the base class part of a B
> object. You do not get freebie access to mess around with the base
> class members of C objects, D objects, E objects or any other class that
> is foreign to B just because it also derives from A.
>
> I hope that is more clear; I don't know how else to say it.
>


ok. I understand your meaning, but I think that's not convincing. In
class A, I change protected to public, then everything is fine. No
errors any longer.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-21-2004
* Andy Ward:
> Given the following code:
> class A
> {
> protected:
> int pro;
> };
>
> class B : public A
> {
> public:
> void foo()
> {
> A * a;
> a->pro;
>
> B * b;
> b->pro2;
> }
> protected:
> int pro2;
> };
>
> The line b->pro2 compiles fine, however the line a->pro does not compile.
> Could someone please explain to me why.


A B-object can only access protected A-stuff in the A-part of B-objects.

'protected' isn't a carte blanche to go messing around inside objects.

It only makes the _definitions_ available, "known", to derived classes,
and if you then create an object of such a derived class, say B, you
(the class B code) can access all that you know in your own object.

On the other hand there's no stopping the determined programmer from
doing evil things.

And sometimes that's necessary (e.g. getting access to the container
inside a std::list).


#include <iostream>

class A
{
protected:
int myValue;
public:
A( int aValue ): myValue( aValue ) {}
};

struct Hack: A
{
static int value( A const* p )
{
return static_cast<Hack const*>( p )->myValue;
}
};

class B: public A
{
public:
static int f()
{
A a( 1234 );

// return a.myValue; -- does not compile.
return Hack::value( &a ); // Oooh, dirty!
}
};

int main()
{
std::cout << B::f() << std::endl; // "1234"
}

--
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
 
Hunter Hou
Guest
Posts: n/a
 
      06-21-2004
Hunter Hou wrote:

>>

>
> ok. I understand your meaning, but I think that's not convincing. In
> class A, I change protected to public, then everything is fine. No
> errors any longer.
>


please ignore this meesage.
 
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 a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 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