简体   繁体   中英

Extending C++ class without modifying the header

Is it possible, in C++ or any other compiled language, to add functionality to a class without modifying the header?

In JavaScript I can add a function to an object after the object is created. Is anything even remote possible in C++?

A use example would be for me to provide a user with some routine.o file, and have them extend it, with something like

void routine::NeverBeforeDeclaredFunction() { ... }

This exact example isn't allowed, but is anything similar? I've thought about letting this class have an array of function pointers, and have a user populate that array with their own function. But this doesn't provide any advantage, such as access to private variables, or access to this .

Is it possible, in C++

No, you cannot do that at runtime nor at compile time in C++. At least not portably without many headaches. You can create a subclass, but then it's not "the same class". If the class is a template class, you could provide your own specializations (but then it's a different type).

C++/CX (a Microsoft language extension to C++) allows for partial classes . This might be a solution to your problem, if you don't care about portability. Note that it is done at compile-time.

or any other compiled language, to add functionality to a class without modifying the header?

Objective-C allows this through categories (compile time) and through the Objective-C runtime (runtime).

Of the compiled languages Objective C is great deal more dynamic, but its facilities are not the same as in Javascript, a language with prototype-based inheritance.

What you can do in C++ is to provide a virtual method taking a std::string , and returning a function pointer that takes an instance of your class as its first argument, for a poor man's version of dynamic dispatch. The only thing that would need to be shared in this instance is the type of the function pointer.

The answer, at least for c++, would be no. The idea of a header is to fully capture the entire interface of the objects/functions being declared. Even if you could do so, it probably wouldn't be good practice.

Member functions (that aren't virtual) all have an implicit "this" parameter which allows them to access their parent object's members, perhaps you could use function pointers which have an explicit "this" parameter. As for access to private members, you can try declaring the functions a friend (unfortunately requires changing the header).

We can add functionality to an existing class by means of operator overloading.

class details 
{
    public:
    std::string name;
};

bool operator==(const details& lhs,const details& rhs)
{
    return(lhs.name==rhs.name);
}

void operator<<(details& lhs,const std::string& json)
{   
    Poco::JSON::Parser parser;
    Poco::Dynamic::Var result = parser.parse(json);
    Poco::JSON::Object::Ptr pObject = result.extract<Poco::JSON::Object::Ptr>();
    lhs.name = pObject->getValue<std::string>("name");    
}

int main(int, char **)
{
    std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }";
    details a,b;
    a==b;//though not a member would behave like one
    a<<json;//i m able to parse json using operator overloading
    std::cout<<a.name<<std::endl;
}

You can use other operators to add functionality.

The question is not between "compiled" vs "not compiled" languages but between "strictly typed" and "weakly typed" languages.

For strictly typed object oriented languages only extension of classes can bring in new methods. That's quite the point of "objects". But extending object also requires, that you have control over the creation of the objects.

However, the need for something like this comes up now and then - and each language comes up with something that allows something which just looks like "expanding" (avoiding "extending" as this is the OO term in this context) existing stuff.

C++ for example uses method overloading for streams, so that this can be made to work:

cout << var_of_some_non_standard_type;

In C# you have Extension Methods - in your own code you can pretend, that a class has some more method. The compiler maps them to static methods.

In Java you can also pretend as little bit by using static imports for functions in your own code .

It depends on what you mean by extending functionality.

Usually, extending functionality translates to extending the class. So, yes , you can derive from the class and add more functionality, while still retaining the type of your base class. Inheritance is a is-a relationship.

If you're referring to adding functionality to the already existing class without extending it or modifying the header, then no, that is impossible. If you ask me, this is a good thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM