0

I am trying to write little type comparing class. This is base class and any derived classes need to override getClassName(), returning it's type name.

class Type
{
public:
    /*Chcek wether type name equals type name of template T*/
    template <class T>
    bool typeof()
    {
        Type* _obj;
        _obj = new T();
        std::string name = _obj->type();
        delete _obj;
        return this->getClassName() == name;
    }

    /*Return type name*/
    std::string type()
    {
        return this->getClassName();
    }
protected:
    virtual std::string getClassName() = 0;
};

Then:

class Derived : public Type
{
protected:
    virtual std::string getClassName()
    {
        return "DerivedFromType";
    }
};

And usage:

Derived foo;
bool isFooDerived = foo.typeof<Derived>();

Seemed to work ok, but in some instances it throws exception - XXX.exe has triggered a breakpoint. on delete _obj;

In stacktrace there is following: HEAP[XXX.exe]: Invalid address specified to RtlValidateHeap(xxxxxxxx, yyyyyyyy)

Am I doing anything wrong regarding that pointer?

Thanks

trincot
  • 317,000
  • 35
  • 244
  • 286
Mirecc41
  • 21
  • 4

0 Answers0