Calling private base methods

Dan Bishop danb_83 at yahoo.com
Thu Apr 12 21:47:37 EDT 2007


On Apr 12, 3:02 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> "7stud" <bbxx789_0... at yahoo.com> wrote:
> > On Apr 12, 5:04 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> >> "7stud" <bbxx789_0... at yahoo.com> wrote:
> >> > On Apr 12, 2:47 am, "Jorgen Bodde" <jorgen.maill... at gmail.com> wrote:
> >> >> Is it possible to call a private base method? I come from a C++
> >> >> background, and I liked this construction as my base class has helper
> >> >> methods so that I do not have to  duplicate code.
>
> >> > I'd like to see some C++ code that does that!
>
> >> Easy:
>
> >> #define private public
> >> #include <someheader>
> >> #undef private
>
> >> then call the private methods as much as you want.
>
> > lol.  I don't see any private methods being created there.
>
> You should have looked in someheader:
>
> class Whatever {
>    private:
>       void ohnoyoudont(int);
>
> }
>
> then back in the C file:
>
> ...
> Whatever foo = new Whatever();
> int ohyesido = 42;
> foo.ohnoyoudont(ohyesido);
> ...
>
> Really, it does work (probably). There are other ways to get at private
> members in C++ but this is the easiest.

I have a job as a C++ programmer and once tried this trick in order to
get at a private member function I needed.  Didn't work: Apparently, VC
++ includes the access level in its name mangling, so you get linker
errors.

After that, I had a much greater appreciation for Python's lack of
"private".




More information about the Python-list mailing list