Re: C++ private/protected hack
bji-ggcpp@ischo.com wrote:
I am the programmer; why *shouldn't* I be allowed to tell the compiler
to do whatever I want it to do in this case?
You can. Simply declare a friend or make everything public. Of course if
you are *not* the programmer of the class in question, you shouldn't be
allowed to decide for him whether you can break his contract.
An analogy would be, if I were some rich and famous person with a
bodyguard, and I gave the bodyguard a rule to follow: "no one is
allowed to ask me for my autograph." And 99.9% of the time, this
worked just how I wanted it to; as people tried to approach me in
public to ask for an autograph, the bodyguard (politely of course)
prevented them from doing so, on my behalf. But then one day, what if
I saw someone I really wanted to give my autograph to came up to me?
Do you think that I would like it if when I told my bodyguard, "hey, I
know I told you before that I didn't want anyone to ask for my
autograph - but this guy over there, he's ok. Let him through", he
responded, "nope, sorry - you told me before that no one can have your
autograph. I don't trust you to change your mind. I won't let him
come to you." No, I would not like that at all! I want the bodyguard
to work *for me*, not *against me*. I want to be able to give him
orders to follow as standing rules so that I don't have to be
instructing him all the time of exactly what to do. But I also want
to be able to override those orders in special circumstances.
Your analogy is apt. If *you* wanted that one person out of a thousand
to come to you, then *you* should be able to tell *your* bodyguard to
let him through. It should not be up to strangers to decide for you that
they are special. You have effectively described the "friend" mechanism.
Your basic problem is that you are assuming that only one programmer is
working on the project. If I'm the only one on the project, then I can
dispense with access specifiers (and in fact do... Python doesn't have
them.)
But I am the programmer. I have every right to decide that I can
refine previous rules that I gave (i.e. turning no one can access this
field, into "no one can access this field except this code over
here").
No, you are *not* the programmer who wrote *my* class. It is not up to
you to summarily decide that *my* contract is void.
Here is what I am trying to do:
* Provide a tool which parses C++ header files and for the class
definitions that it finds, generate "tables" (static C structures)
which can be fed to a runtime initialization routine to produce C++
objects which fully describe these classes. When I say "fully
describe", I mean give every knowable detail about the class. And
also - provide generic accessor methods for accessing all of the
fields and methods of a class.
* Use this information in a variety of ways, the first and foremost
being, to implement a serialization library. This library could be
implemented *one time*, as a library that could be linked into any
application willing to use the tool that I mentioned above, and that
would be able to serialize *any* class (note 1) given a pointer to an
instance of that class, and the extended type information that the
tool generated.
(note 1: it can't really work for *any* class. There are some rules
about how the class could be defined that would have to come into
play; I wish it weren't so, but it is)
What you want can't be done without the explicit help of the designers
of the classes. Even you admit that in your "note" above. So you want
the designers to obey a whole host of rules about how the class can be
defined but you don't want one of those rules to be "let so-and-so class
be a friend." What you are asking for is silly.
I personally think that this would be a compelling tool; it would
allow me to never again have to even *think* about writing
serialization logic for any class that I ever define. I just have to
use the tool, and the library, and viola, I have serialization support
for any class I would ever care to write.
You have that all wrong. The tool would *require* you to constantly
think about writing your classes such that they will be compatible with
the tool.
I think this is a fantastic idea, except that it violates one rule
that I was hoping *so hard* to not have to violate: it requires
developers to decorate their class definitions with special method
declarations to support this.
But, by your own admission above, your tool already requires developers
to conform to a whole host of rules in order to be compatible with it
*even if everything is public*!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]