const引用和const指针上的new-expression和delete-expression [英] new-expression and delete-expression on const reference and const pointer

查看:154
本文介绍了const引用和const指针上的new-expression和delete-expression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++

许多文献说,const引用不能用于修改其引用,并且const指针不能用于修改其指针.

Much literature says const references cannot be used to modify their referents and const pointers cannot be used to modify their pointees.

然后,为什么要delete d?

const int& cirDynamic = *( new int(5) );
// ^ 'const int& cirDynamic = *( &( *( new int(5) ) ) );' gives same output below
cout << cirDynamic << endl; // 5
delete &cirDynamic;
cout << cirDynamic << endl; // garbage value

我知道T* const中的尾随const仅阻止重新放置指针,但是下面我像const T* const一样使用两个const进行强调.为什么以下指针是delete d?

I know the trailing const in T* const only prevents the pointer from being reseated, but below I use two consts, as in const T* const, for emphasis. Why can the following pointer be deleted?

const int* const cipcDynamic =  new int(5);
// ^ 'const int* const cipcDynamic = &( *( new int(5) ) );' gives same output below
cout << *cipcDynamic << endl; // 5
delete cipcDynamic;
cout << *cipcDynamic << endl; // garbage value

输出显示至少释放了一些动态分配的内存.是否全部释放了它,或者只有释放了副本的地方才涉及复制?

The output shows that at least some dynamically allocated memory was freed. Has all of it been freed, or could there have been copying involved where only the copy was freed?

const参考代码段(int&)的非const版本和const指针const代码段(int* constint*)的非前导const版本产生的输出与其他const对应代码相同.在这5种情况下,为什么以及如何延长临时new表达式的生存期?

The non-const version of the const reference snippet (int&) and the non-leading-const versions of the const pointer const snippet (int* const and int*) produce the same output as their more const counterparts. In all 5 cases, why and how is the lifetime of the temporary new-expression extended?

如果数据类型是类或结构,假设相应的运算符没有被重载,显式删除或变得不公开,那么标准是否做以下保证:

Assuming the corresponding operator has not been overloaded, explicitly deleted, or made non-public if the data type is a class or struct, does the Standard make the following guarantees:

  • 解除引用运算符可直接访问指针对象

  • The dereference operator provides direct access to the pointee

new运算符生成指向动态分配内存的指针,而不是原始动态分配内存的动态分配副本

The new operator produces a pointer to the dynamically allocated memory, not a dynamically allocated copy of the original dynamically allocated memory

如果new运算符被重载但仍返回::operator new(size),而取消引用运算符被重载但仍返回对该对象的引用,是否有任何副作用使这两个点不成立?

If instead the new operator was overloaded but still returned ::operator new(size) and the dereference operator was overloaded but still returned a reference to the object, are there any side-effects that would make these two points not hold?

推荐答案

常量会影响对象本身. newdelete以及构造函数会影响对象的 creation .询问构造函数或析构函数是否为const是没有意义的,因为它们在对象存在之前或之后运行.同样,您可以动态创建和销毁常量对象,和/或可以通过常量指针或引用管理动态创建的对象.

Constness affects objects themselves. new and delete and constructors affect the creation of objects. It doesn't make sense to ask whether constructors or destructors are const, because they run before or after the object exists. Similarly, you can create and destroy constant objects dynamically, and/or you can manage dynamically created objects through constant pointers or references.

作为一个非常简单的思想实验,请考虑以下代码:

As a very simple thought experiment, consider this code:

{
    const int x = 0;
}

如果常量可以防止对象x被破坏,则此方法将无效.

This wouldn't work if constness could prevent the object x from being destroyed.

这篇关于const引用和const指针上的new-expression和delete-expression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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