C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Impose friendship when needed

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Mon, 18 Jul 2022 12:26:47 +0200
If the library was compiled in binary form, changing the access modifier can break compatibility of class layout. But under controlled circumstances this could be a successful hack (and its success can be automatically tested with offsetof, etc., for each class member)   -----Ursprüngliche Nachricht----- Von:Peter C++ via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mo 18.07.2022 07:06 Betreff:Re: [std-proposals] Impose friendship when needed An:std-proposals_at_[hidden]; CC:Peter C++ <peter.cpp_at_[hidden]>; i agree with most of the opposing authors wrote. however, instead of editing the header, have you considered using the preprocessor instead: #define private public #include "header" #undef private sent from a mobile device so please excuse strange words due to autocorrection. Peter Sommerlad peter.cpp_at_[hidden] +41-79-432 23 32 > On 17 Jul 2022, at 20:01, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote: > >  > Recently I was working with an RS232 library when programming a microcontroller in C++. I needed a little finer control over the library's inner workings, more control than the library provided. I had to edit the library's header file to change the visibility of one of the class's member variables, I changed it from 'private' to 'public', and also I made one of the member functions 'public'. Alternatively I could have added one line at the end of the class definition: > >     friend class MyOwnClass; > > and then I could have accessed all the private members through static member functions belonging to MyOwnClass. > > I propose that we should be able to "impose friendship" without editing the library's header files. For example: > >     class Foo { int monkey; }; > >     int main(void) >     { >         Foo obj; > >         using friend Foo; > >         obj.monkey = 5; >     } > > The statement "using friend Foo" only has an effect within 'main', similar to how "using namespace std" would have limited scope here. > > Alternatively, the syntax could be on an access-by-access basis, something like as follows: > >     int main(void) >     { >         Foo obj; > >         friend<obj>.monkey = 5; >     } > > or the syntax could be: > >     friend<obj.monkey> = 5; > > or maybe: > >     std::friend(obj, monkey) = 5; > > or: > >     friend >> obj.monkey = 5; > > Some of you might be thinking: "Why affirm such subterfuge when the author of the library obviously didn't want you messing around in there?", well computer programming isn't an exact science. Indeed we have paradigms and design patterns, but really at the end of the day we're trying to get things done, and this might mean following the instructions from Step 1 to Step 9 but then deviating at Step 10. > > Being able to manually impose friendship when and where you need it, would mean not needing to edit the 3rd party library's header file. > -- > Std-Proposals mailing list > Std-Proposals_at_[hidden] > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-07-18 10:26:49