Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从运算符new/delete中获取有关对象的RTTI信息_C++_New Operator_Rtti - Fatal编程技术网

C++ 从运算符new/delete中获取有关对象的RTTI信息

C++ 从运算符new/delete中获取有关对象的RTTI信息,c++,new-operator,rtti,C++,New Operator,Rtti,假设我想在下一个类之后派生所有内容,并且我不假设new或delete有任何进一步的重载: class Object { public: static map<std::string, unsigned int> typeDeltaMap; void* operator new(size_t size) { void* p = ::new char[size]; const

假设我想在下一个类之后派生所有内容,并且我不假设new或delete有任何进一步的重载:

class Object
{
public:
        static map<std::string, unsigned int> typeDeltaMap;
        void* operator new(size_t size)
        {
                void* p = ::new char[size];
                const std::string type = typeid(this).name(); //compile error
                cout << "new type=" << type << endl;
                ++typeDeltaMap[type];
        }
        void operator delete(void* p)
        {
                ::delete(p);
                const std::string type = typeid(this).name(); //compile error
                cout << "delete type=" << type << endl;
                --typeDeltaMap[type];
        }
};
我不工作,因为new和delete是静态的,但是有一些特殊的静态,因为它们同时是虚拟的。
我的问题是是否有办法获得这些信息?

正如@Someprogrammerdude建议的那样,我已经使用了满足我需求的CRTP

template<typename T>
class Object
{
public:
        ;
        void* operator new(size_t size)
        {
                const std::string type = typeid(T).name();
                cout << "new type=" << type << endl;
                void* p = ::new char[size];
                return p;
        }
        void operator delete(void* p)
        {
                const std::string type = typeid(T).name();
                cout << "delete type=" << type << endl;
                ::delete(p);
        }
};

class A : public Object<A>
{
public:
virtual ~A(){}
};

class B : public Object<B>
{
public:
virtual ~B(){}
};

int main()
{
        A* a = new A();
        B* b = new B();
        delete a;
        delete b;
}
模板
类对象
{
公众:
;
void*运算符新(大小\u t大小)
{
const std::string type=typeid(T).name();

我能想到的唯一方法是。当调用
运算符new
时,对象还不存在。当调用
运算符delete
时,它已经被销毁。你需要自己传播类型信息。@Someprogrammerdude CRTP没有给出一个公共的基类型,至少我知道不是这样。
的基类>B
将是
A
@TheVee似乎OP想要的是派生类型,而不是基类型,这正是CRTP提供的。@有些编程人员当然想,但同时他们想通过指向基的指针存储对象,请参见
main
函数。
template<typename T>
class Object
{
public:
        ;
        void* operator new(size_t size)
        {
                const std::string type = typeid(T).name();
                cout << "new type=" << type << endl;
                void* p = ::new char[size];
                return p;
        }
        void operator delete(void* p)
        {
                const std::string type = typeid(T).name();
                cout << "delete type=" << type << endl;
                ::delete(p);
        }
};

class A : public Object<A>
{
public:
virtual ~A(){}
};

class B : public Object<B>
{
public:
virtual ~B(){}
};

int main()
{
        A* a = new A();
        B* b = new B();
        delete a;
        delete b;
}