[Qt-interest] [OT] RE: QWizardPage::wizard() overprotected?

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Fri Dec 12 14:07:48 CET 2008


Niels Dekker - no return address wrote on Friday, December 12, 2008 1:45 PM:

> Oliver Knoll wrote:
>>>>> #define private public
>>>>> #define protected public
>
>>> I can confirm that this still works with Visual Studio 2005
>>> (Express)
>
> On VC++ 2008 SP1, it gets me those link errors ...
> ...
> Thanks, Oliver! There's the C++ Standard citation: "Nor shall such a
> translation unit define macros for names lexically identical to
> keywords".

Just to make clear the obvious: I wasn't really suggesting the "#define private public"-HACK as a serious solution ;)

>> Anyway, I got this dirty trick from some C++ guru...
>
> He must have been mistaken (or maybe he was just joking?).  Even
> guru's can make mistakes, you know!

Well, the context was whether one could easily get around (read: "cheat") the "private" and "protected" mechanisms in C++. While I was thinking of tweaking the function pointers in the virtual function pointer tables (in case the methods in question were virtual, that is), Detlef (that was his name) simply mentioned "Well, it's much easier than you think, you'd just say #define..." And I was going "Whot!??" 0.0

And that was that :) The fact whether it was standard-compliant or not was never part of that discussion (that's rarely the case with "really dirty tricks"), but obviously compilers didn't enforce this C++ keyword rule until recently (VS 2008 for example - but it is really just the linker which apparently fails, because the "visibility" gets encoded into the function name - so the "enforcement" of this rule might just be a side-effect of something else. Else it would already be the compiler which would complain about the C++ standard violation...).

Btw in Java you can do that as well, using "introspection" and playing around with the access modifiers or so, if you want to call a private/protected method from outside the class. So yes, also Java allows you to shoot into your foot, even though it is much more complicated and tedious ;) I have seen that in unit tests, when testing private methods (whether you should unit-test private methods is another discussion... ;)


But all that said, when the Qt API doesn't make a method public, then there's mostly a good reason for it, and you should think whether your solution (requiring a certain method to be public) is pointing into the right direction! Mostly there is a better way to solve things...


Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22




More information about the Qt-interest-old mailing list