gpt4 book ai didi

C++11 删除类类型?

转载 作者:IT老高 更新时间:2023-10-28 23:00:59 26 4
gpt4 key购买 nike

在 C++11 N3485 5.3.5.1 中它说:

The operand [of delete] shall be a pointer to object type or a class type. If of class type, the operand is contextually converted to a pointer to object type.

这种用法的例子是什么(操作数是类类型)?

最佳答案

If of class type, the operand is contextually implicitly converted to a pointer to object type.

因此,您可以在对象上使用 delete,但当且仅当此类型具有隐式转换运算符到指针时。

class A
{
public:
class Inner {};
A()
{
inner = new Inner();
}
operator Inner*() { return inner; }
private:
Inner* inner;
};

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

不过,这不是 C++11 的新特性,在 C++03 标准中几乎有相同的词

The operand shall have a pointertype, or a class type having a single conversion function (12.3.2) to a pointer type.

关于C++11 删除类类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16585713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com