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

Are there member functions which are static by default?

重载的 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 是否真的是静态的。

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

这在[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).

是的,static 关键字对于 operator new 是可选的,当它被定义为 class 的成员函数时,它将始终是 static 成员函数。

(强调我的)

Both single-object and array allocation functions may be defined as public static member functions of a class (versions (15-18)). If defined, these allocation functions are called by new-expressions to allocate memory for single objects and arrays of this class, unless the new expression used the form ::new which bypasses class-scope lookup. The keyword static is optional for these functions: whether used or not, the allocation function is a static member function.