samedi 7 octobre 2017

Overriding function behaviour in a custom smart pointer in c++

First of all i want so say that i am not even sure if this is possible to implement.

I have a base class and a closure, the base class always gets extended by a specific class, but its not determined what the base class specificly contains.

Now i want to override the behavior of the base class while changing the behaviour of the specific class.

Here is a simple example of what i am trying to achieve:

class base
{
    public:
        inline void print()
        {
            std::cout << "test!" << std::endl;
        }
};

template <class T>
class custom_ptr
{
    public:
        inline custom_ptr()
        {
            if (!std::is_convertible<T*, base*>::value) {
                throw "error!";
            }

            this->baseInstance = new T();
        }

        /*
         * In this class the behavior of test should be extended/overridden.
         * in this case base::print
        */

        ~custom_ptr()
        {
            delete this->baseInstance;
        }

        inline T * operator-> ()
        {
            return this->baseInstance;
        }
    private:
        T * baseInstance;
};

class test : public base
{
    public:
        inline void otherStuff()
        {
            /* ... **/
        }
        /* .. */
};

In this case i want to override base::print(), but maintain the full functionality of test::otherStuff().

But dont missunderstand me, when this function gets called without the closure then it should maintain his bevaviour as given.

Aucun commentaire:

Enregistrer un commentaire