Is it possible to add new functions in the class if the implementation of this class is hidden to user. But still if its required to add a new function to this class.
-
Derive a new class from it, add new methods to that class? – Govind Parmar Feb 11 '19 at 18:41
-
5Probably an XY problem - why do you think you need to do this? – Feb 11 '19 at 19:17
-
take a look at https://en.wikipedia.org/wiki/Adapter_pattern – AndersK Feb 11 '19 at 19:53
2 Answers
"Is it possible to add new functions in the class if the implementation of this class is hidden" - In general, No.
To add new functions you have some options:
1) Get hold of the original source code and add your function (best option).
2) Derive from the class (if it is not final) and add your function (only good enough if you can then use the derived type everywhere needed).
3) Write a free function that takes a pointer or reference to an instance of the class and then uses that to access (public) members of the class. Not really like adding a real member, but in some cases good enough.
You cannot dynamically add functions to a class in C++ like you can in some other languages.
- 30,449
- 3
- 47
- 70
Is it possible to add new functions in the class if the implementation of this class is hidden
Yes, you can add new functions to the class declaration. C++ does not demand that all member functions are implemented in the same compilation unit.
Adding virtual functions or member variables could be a problem, if the class is instantiated in code that was compiled with the 'old' class declaration.
- 6,037
- 2
- 18
- 24