Add a method to existing C++ class in other file

No. This kind of class extension is not possible in C++. But you can inherit a class from the original source file and add new functions in your source file.

class Abc {
    void methodOne();
    void methodTwo();

class AbcExt : public Abc {       
    void methodAdded();

You can then call methods as following:

std::unique_ptr<AbcExt> obj = std::make_unique<AbcExt>();
obj->methodOne(); // by the virtue of base class
obj->methodAdded(); // by the virtue of derived class

Leave a Comment