* C++ Protected Class Members are Private???
@ 2003-02-19 19:47 Mongryong
2003-02-19 21:05 ` Neil Booth
0 siblings, 1 reply; 6+ messages in thread
From: Mongryong @ 2003-02-19 19:47 UTC (permalink / raw)
To: gcc
Okay, I'm not sure what the spec says, but GCC treats 'protected' member
variables/methods as private in the following example:
class BinaryTree {
protected:
Object* key;
BinaryTree* left;
BinaryTree* right;
//....
};
class BST: public BinaryTree {
public:
void insert(Object& obj) {
//....
}
//....
};
class AVLTree: public BST {
protected:
void balance () {
//...
// LL rotation
right = left;
// now, this generates a compiler error!!!
// right->left: left is protected and can't be accessed??
left = right->left;
}
pubic:
void insert (Object& obj) {
BST::insert(obj);
balance();
}
//...
};
So, is this a bug? Thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C++ Protected Class Members are Private???
2003-02-19 19:47 C++ Protected Class Members are Private??? Mongryong
@ 2003-02-19 21:05 ` Neil Booth
2003-02-19 23:17 ` Mongryong
2003-02-20 10:12 ` Gabriel Dos Reis
0 siblings, 2 replies; 6+ messages in thread
From: Neil Booth @ 2003-02-19 21:05 UTC (permalink / raw)
To: Mongryong; +Cc: gcc
Mongryong wrote:-
> // now, this generates a compiler error!!!
> // right->left: left is protected and can't be accessed??
> left = right->left;
left = this->left is OK, but not right->left because it's a different
object. At least, that's my understanding. Welcome to C++.
Neil.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C++ Protected Class Members are Private???
2003-02-19 21:05 ` Neil Booth
@ 2003-02-19 23:17 ` Mongryong
2003-02-19 23:41 ` Neil Booth
2003-02-20 10:34 ` Nathan Sidwell
2003-02-20 10:12 ` Gabriel Dos Reis
1 sibling, 2 replies; 6+ messages in thread
From: Mongryong @ 2003-02-19 23:17 UTC (permalink / raw)
To: Neil Booth; +Cc: gcc
On Wed, 2003-02-19 at 15:54, Neil Booth wrote:
> Mongryong wrote:-
>
> > // now, this generates a compiler error!!!
> > // right->left: left is protected and can't be accessed??
> > left = right->left;
>
> left = this->left is OK, but not right->left because it's a different
> object. At least, that's my understanding. Welcome to C++.
>
This has got to be a compiler issue. The compiler knows the context and
scope from which 'right->left' is called and should be able to determine
that it's 'okay'. It's kind of dumb for C++ to define this behaviour as
not okay.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C++ Protected Class Members are Private???
2003-02-19 23:17 ` Mongryong
@ 2003-02-19 23:41 ` Neil Booth
2003-02-20 10:34 ` Nathan Sidwell
1 sibling, 0 replies; 6+ messages in thread
From: Neil Booth @ 2003-02-19 23:41 UTC (permalink / raw)
To: Mongryong; +Cc: gcc
Mongryong wrote:-
> On Wed, 2003-02-19 at 15:54, Neil Booth wrote:
> > Mongryong wrote:-
> >
> > > // now, this generates a compiler error!!!
> > > // right->left: left is protected and can't be accessed??
> > > left = right->left;
> >
> > left = this->left is OK, but not right->left because it's a different
> > object. At least, that's my understanding. Welcome to C++.
> >
> This has got to be a compiler issue. The compiler knows the context and
> scope from which 'right->left' is called and should be able to determine
> that it's 'okay'. It's kind of dumb for C++ to define this behaviour as
> not okay.
No, it's a language issue.
Neil.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C++ Protected Class Members are Private???
2003-02-19 21:05 ` Neil Booth
2003-02-19 23:17 ` Mongryong
@ 2003-02-20 10:12 ` Gabriel Dos Reis
1 sibling, 0 replies; 6+ messages in thread
From: Gabriel Dos Reis @ 2003-02-20 10:12 UTC (permalink / raw)
To: Neil Booth; +Cc: Mongryong, gcc
Neil Booth <neil@daikokuya.co.uk> writes:
| Mongryong wrote:-
|
| > // now, this generates a compiler error!!!
| > // right->left: left is protected and can't be accessed??
| > left = right->left;
|
| left = this->left is OK, but not right->left because it's a different
| object. At least, that's my understanding. Welcome to C++.
Your understanding is correct. BinaryTree didn't grant friendship to
AVLTree, therefore any direct access to a BinaryTree member from an
AVLTree member function is invalid.
-- Gaby
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C++ Protected Class Members are Private???
2003-02-19 23:17 ` Mongryong
2003-02-19 23:41 ` Neil Booth
@ 2003-02-20 10:34 ` Nathan Sidwell
1 sibling, 0 replies; 6+ messages in thread
From: Nathan Sidwell @ 2003-02-20 10:34 UTC (permalink / raw)
To: Mongryong; +Cc: Neil Booth, gcc
Mongryong wrote:
> On Wed, 2003-02-19 at 15:54, Neil Booth wrote:
>
>>Mongryong wrote:-
>>
>>
>>> // now, this generates a compiler error!!!
>>> // right->left: left is protected and can't be accessed??
>>> left = right->left;
>>
>>left = this->left is OK, but not right->left because it's a different
>>object. At least, that's my understanding. Welcome to C++.
>>
>
> This has got to be a compiler issue. The compiler knows the context and
> scope from which 'right->left' is called and should be able to determine
> that it's 'okay'. It's kind of dumb for C++ to define this behaviour as
> not okay.
As Neil says, it is the language. To access protected members of a base
you have to go through the this pointer (this is *harder* to implement
than the semantics you expected.) To see this from the std you need both [11.2]/4
[11.5]/1
No, I don't know the rationale for it.
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
The voices in my head said this was stupid too
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-02-20 10:12 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-19 19:47 C++ Protected Class Members are Private??? Mongryong
2003-02-19 21:05 ` Neil Booth
2003-02-19 23:17 ` Mongryong
2003-02-19 23:41 ` Neil Booth
2003-02-20 10:34 ` Nathan Sidwell
2003-02-20 10:12 ` Gabriel Dos Reis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).