适当地重载new/delete new []/delete [] [英] Properly Overloading new/delete new[]/delete[]

查看:72
本文介绍了适当地重载new/delete new []/delete []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个问题的跟进,

This is a follow up to my previous question,

使用malloc初始化类

问题的可接受答案有效,并在avr-gcc上给我了new/delete,这是问题所在,但是我的重载new delete破坏了常规gcc,重载new delete的正确方法是什么?一个通用的基类,因此理想情况下,我只想为我的对象覆盖new delete,这样它就不会与stl stdlib等混淆.

Accepted answer on the question works and gives me new/delete on the avr-gcc, here is the problem but my overloaded new delete wracks havoc on regular gcc, what is the proper way to overload new delete all my classes derive from a common base class so ideally i would like to just override new delete for my object so it does not mess with stl stdlib etc.

推荐答案

'new'和'delete'可以在常见的Object基类中重载.因此,这将仅适用于该层次结构.

'new' and 'delete' can overloaded inside the common Object base class. So, that will be applicable only to that hierarchy.

class Object {
public:
  void* operator new (size_t size);
  void operator delete (void *p);
};

class Derived : public Object {
// uses the above versions of new/delete
};

[注意:这对您来说是一个额外的优势,因为您所有的类都从一个常见的Object类派生(如您的问题和链接中所述)]

[Note: It's an added advantage for you as all your class are getting derived from a common Object class (as mentioned in your question and the link)]

这篇关于适当地重载new/delete new []/delete []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆