// Is the following code well-defined?
[snip]
Of course it is. Also, there's no violation of access scope as the
title seems to imply.
Yes; it is similar in spirit to doing something like this:
class A {
public:
int & value (void) { return _value; }
private:
int _value;
};
Where, by returning a reference to a private member through a public
function, you let a caller modify the "private" member. Having things
be "private" or "protected" doesn't really put any code or data in a
magical protected area of memory or something; you can still access
them from anywhere. "Private" and "protected" exist to help a
programmer enforce access rules at compile time, and also to add some
convenient rules to how derived classes inherit access to member
variables (in the case of "private" vs. "protected"). You can still
access the data from anywhere though.
Think of it like... you have an XBox 360 in your living room. It's
declared as "private", so people can't just walk in and play it
whenever they feel like it. However you can send out a public
invitation to your friends, and give them full access to your private
XBox. Not a problem. This is similar to providing public function
pointers to private functions or public access to private members. Of
course somebody could just break into your house and start playing
your video games -- that's more equivalent to accessing private data
by accessing the instance's block of memory directly instead of by
named members.
Jason