c++ - 是否有默认为静态的成员函数?

标签 c++ operator-overloading static-methods

重载的 operator new 默认被认为是静态的吗?例如:

template <typename E>
class Link
{
private:
    Link<E>* freeList;
public:
    E element;
    Link<E>* next;
    Link(const E& elemVal, Link<E>* nextVal) { element = elemVal; next = nextVal; }
    Link(Link<E>* nextVal) { next = nextVal; }

    void* operator new(size_t)
    {
        Link<E>* temp=freeList;
        freeList=freeList->next;
        return temp;
    }
};

当我尝试编译它时,出现以下错误:

Invalid use of member 'Link<E>::freeList' in static member function.

我想知道重载的 operator new 是否实际上是静态的。

最佳答案

是的!特定于类的 operator newoperator delete 的重载是静态成员函数。它们不能是“常规”成员函数,因为在调用它们时没有可用的实例。他们在那里(取消)分配原始存储。对于它们中的任何一个,此时都没有对象实例。

这在 [class.free]/1 中有描述:

Any allocation function for a class T is a static member (even if not explicitly declared static).

[class.free]/5 :

Any deallocation function for a class X is a static member (even if not explicitly declared static).

关于c++ - 是否有默认为静态的成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51581413/

相关文章:

c++ - 使用boost线程的多线程中的段错误(核心转储)

C++ NetBeans : How to link my . o 文件到我的项目?

c++ - 函数名称周围的括号如何更改调用哪个函数?

php - 获取父类中子类的名称(静态上下文)

java - 使用 'any' 使用 JMockit stub 静态方法

c# - 静态类优缺点

c++ - 架构 armv7s : iPhoneExtAudioFileConvertTest 的 undefined symbol

c++ - 逐行读取文本文件,然后逐字 C++,推送到数组

c++ - 重载运算符不抛出异常

c++ - 将类与运算符 [] 一起使用