C++ Class Extension

No. C++ has no such capability.

As mentioned in other answers, the common workarounds are:

  • Define a derived class, perhaps with a factory to hide the actual implementation class
  • Define a decorator class
  • Define non-member functions that operate on instances of the class

C# class extension methods are mostly syntactic sugar. You get the same functionality with free functions (i.e., functions with a reference or constant reference to your class as their first parameter). Since this works well for the STL, why not for your class?


No, you can't do this in C++.

If you want to achieve something like this you have 2 options,

  • You could inherit from the class (if this is an option, it might not be legal as the class may not have been written to allow inheritance)
  • You can write your own wrapper class that has the same interface + your new methods and delegate to the one you want to extend.

I prefer the delegation approach.