[Exceptional C++ Style] Re: Item 15 -UsesandAbuses of Access Rights

Balog Pal pasa at lib.hu
Tue Dec 14 10:09:34 EST 2004


>> That IMHO leaves less chance for the compiler to break anything -- and if
>> done everywhere not even breaks the ODR.  As the #define for is 
>> considered
>> harmless this should be similar.
>
> I'm shocked! Why are you even contemplating this? Sure it's just plain 
> evil?

We're not contemplating but solving puzzles the best we can. ;-)

> Anyway, what about if your class had more than one private section?

Shall be no problem. (I guess, not tested. friend declarations are 
declarations and shall be allowed to multiplicate.)

> I have to be honest, until I read this item I had never thought of doing 
> any
> of the things it explores. Possibly it may do more harm than good....?

No-one said we shall do that -- in fact the guideline is stated as to avoid 
this even the completely legal and portable methods.

Generally if there's a need to use some such trick indicates a design 
problem.  And is thall be solved by redesigning stuff.   The RL problem is 
you can have the problem in a non-modifiable library.  And pointing out that 
the $@#@$@ library vendor screwed up will not make your job done.

Eg. time to time I face such problems with MFC. A kind of at least, there a 
plenty of members are protected instead of private, and supposed to be 
accessed from derived classes.  But there's also need do have a plenty of 
free functions (callbacks and such). Even generic ones that works with refs 
to the base class.

The method I use there is to define a derived class with just a set of 
nonvirtual accessors, and static_cast the instance to that class.  That is 
ceartainly too forbidden standard-wise.  But this method of extending a 
class' interface does not only appear to work with MSVC, but is present in 
several samples provided by MS.

Also a great need for such hacks appears when you write log, trace, debug 
info. To track down some prolem.  The debugger shows you the state, but 
sometimes it is impossible to stop (as there are threads, messages, 
serial/socket communication, database ops running) and look by hand -- still 
you need the same level of info.   Is that really evil? :-) 




More information about the Effective-cpp mailing list