user1899020
user1899020

Reputation: 13575

How to build a wrapper on a class hierarchy?

I have an existing class hierarchy. I want to write a wrapper on it, which renames the old classes and methods in the old classes. Is it possible to implement it? For example

class OldBase
{
public:
    void OldMethod();
};

class OldDerive : public OldBase
{
public:
    void OldMethod();
};

Replace "Old" names to "New" names but using the old existing implementations.

Upvotes: 0

Views: 94

Answers (1)

Paweł Stawarz
Paweł Stawarz

Reputation: 4012

You have a lot of ways to do what you want, depending on your real needs.

  1. The first (and probably worst) one is to use #define to rename your functions. It has a lot of flaws, but it can be fine, if your class hierarchy isn't big and you're not doing a big project using the wrapper.

  2. You can also implement the wrapper as a class. And that's probably the best way.

     class wrapper{
     private:
        OldBase *base;
        wrapper(){base=0;};
     public:
        void newMethod();
        wrapper(OldBase* wrappedObject);
     };
    

Then you just do a wrapper by doing wrapper myWrapper(&myBaseObj);, and use it however you want. Of course you can also pass any derived class into the wrapper, or even design a constructor which takes a reference, instead of a pointer, and takes the address inside the constructor.

Upvotes: 2

Related Questions