1

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.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
LearningCpp
  • 802
  • 7
  • 25

2 Answers2

8

"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.

Jesper Juhl
  • 1
  • 3
  • 38
  • 63
-2

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.

Sid S
  • 5,887
  • 2
  • 12
  • 23