1

I'd like to make some classes become friends of other classes at runtime. Is there a way to do this?

Alternatively, is there a way to access an object's public/protected/private ivars at runtime - or extend a class similar to Objective-C's category feature?

The use case here is that I'd like to be able to give a testing suite access to ivars in classes at runtime - but without polluting the application code with test suite specific code. The test framework could change, and will be different depending on the deployment platform.

Summary: find a way to keeping the application code clean and free from external framework artifacts.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    What is it that you are trying to do that would require you to change friendship at runtime? The concept of access modifiers doesn't exist at runtime. – François Andrieux Nov 01 '18 at 21:41
  • 2
    Sounds like an [XY Problem.](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Edit your question to outline your use case and we may be able to offer an alternative that is valid in C++. – user4581301 Nov 01 '18 at 21:43
  • user4581301 I've added a use case to the original question, thanks for the suggestion. – user1824607 Nov 01 '18 at 21:54
  • https://stackoverflow.com/questions/11486622/how-is-access-for-private-variables-implemented-in-c-under-the-hood – drescherjm Nov 01 '18 at 21:57
  • @user1824607 Runtime doesn't seem to come into play here. Or at least I don't see how. Are you trying to load a dynamic library at runtime and want access to the private members? – François Andrieux Nov 01 '18 at 22:09
  • This is opinion so take it with a grain of salt. I come from a school of thought that believes tests should not know private implementation details. If a test can do things that the program that will use the code under test cannot do, the test is not testing the true use cases and the results of the testing are questionable. – user4581301 Nov 02 '18 at 17:42
  • *"is there a way to access an object's public/protected/private ivars at runtime..."* - Yes. ObjC and C++ are not like .Net; the runtime does not enforce visibility. So just change the other class header to suit your taste. Also see [How do I test a private function or a class that has private methods, fields or inner classes?](https://stackoverflow.com/q/34571/608639), [Testing private class member in C++ without friend](https://stackoverflow.com/q/11458223/608639), (and friends). – jww Nov 03 '18 at 18:06

1 Answers1

4

I'd like to make some classes become friends of other classes at runtime. Is there a way to do this?

No that's not possible with c++.

Alternatively, is there a way to access an object's public/protected/private ivars at runtime

There's no concept of changing visibility of class members at runtime also.

Access modifiers are pure compile time concepts, there's no way to change this at runtime.


A way to overcome this for you specific use-case might be the use of interfaces as I've been sketching out here:

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190