const_cast 的行为

发布于 2025-01-02 10:25:34 字数 1150 浏览 1 评论 0原文

我正在阅读有关 C++ 中的 const_cast 运算符

1.我无法理解的第一件奇怪的事情是

const_cast 运算符语法即

<块引用>

-const_cast--<--类型-->--(--表达式--)--------------------><< /p>

我对这种语法的理解是,它有助于消除类型 Type表达式的常量性。但是请考虑

class  ConstTest {   

private:
    int year;
public:
    ConstTest() : year(2007) {}
    void  printYear() const;
};

int main() {
    ConstTest c;
    c.printYear();
    return  0;
}

void ConstTest::printYear() const {
    ConstTest  *c  = const_cast<ConstTest*>(this);
    c->year  = 42;
    std::cout  <<  "This  is the  year "  << year  << std::endl;
}

以下代码:ConstTest *c = const_cast(this),我认为this指针的const应该被丢弃,但输出显示它是this的对象code> 指的是失去它的常量。

我觉得代码应该是 ConstTest *c = const_cast(*this),但这会产生错误。我知道我的很多解释都是错误的。请大家指正。

2.我的第二个问题是下面给出的语句

const_cast 表达式的结果是右值,除非 Type 是引用类型。在这种情况下,结果是左值。

为什么会这样,为什么在指针的情况下却不是这样?

I was reading about const_cast operator in c++

1.First weird thing thing i can't understand is

const_cast operator syntax i.e.

-const_cast--<--Type-->--(--expression--)--------------------><

what i have understand about this syntax is that it helps to cast away constness of anexpressionof type Type .But consider this code

class  ConstTest {   

private:
    int year;
public:
    ConstTest() : year(2007) {}
    void  printYear() const;
};

int main() {
    ConstTest c;
    c.printYear();
    return  0;
}

void ConstTest::printYear() const {
    ConstTest  *c  = const_cast<ConstTest*>(this);
    c->year  = 42;
    std::cout  <<  "This  is the  year "  << year  << std::endl;
}

Here in line ConstTest *c = const_cast<ConstTest*>(this), I think that the const of this pointer should be cast away, but the output shows that it is the object which this refers to that loses its const-ness.

I feel that the code should have been ConstTest *c = const_cast<ConstTest>(*this), but this produces an error. I know i am wrong at many interpretations. Please correct them all.

2.my second problem is the statement given below

The result of a const_cast expression is an rvalue unless Type is a reference type. In this case, the result is an lvalue.

Why is this so, and why it is not true in case of pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2025-01-09 10:25:34

它有助于消除 Type 类型表达式的常量性

否,Type 是结果的类型,而不是操作数的类型。

我认为这个指针的 const 应该被抛弃

this 具有类型 const ConstTest*const_cast(this) 的类型为 ConstTest*。这就是从指向 const 的指针中“抛弃 const”的含义。

我觉得代码应该是ConstTest *c =
const_cast(*this)

const_cast 的结果具有类型 T,这就是它的定义方式。也许你会以不同的方式定义它,但运气不好,你不会通过编写 const_cast 获得 ConstTest*,而是通过编写 const_cast< 获得它;ConstTest*>。您的首选语法不可用。

您可以执行 ConstTest &c = const_cast(*this)ConstTest *c = const_cast(this),所以选择你最喜欢的。

const_cast 表达式的结果是右值,除非 Type 是
参考类型。在本例中,结果是左值。

为什么会这样以及为什么在指针的情况下不成立?

对于指针来说也是如此。 ConstTest* 不是引用类型,并且 const_cast(this) 的结果是右值。然后将该值分配给变量c

it helps to cast away constness of an expression of type Type

No, Type is the type of the result, not the type of the operand.

What i think is const of this pointer should be casted away

this has type const ConstTest*. const_cast<ConstTest*>(this) has type ConstTest*. That's what "casting away const" from a pointer-to-const means.

I feel code should have been ConstTest *c =
const_cast<ConstTest>(*this)

The result of const_cast<T> has type T, that's how it's defined. Maybe you would have defined it differently, but tough luck, you don't get a ConstTest* by writing const_cast<ConstTest>, you get it by writing const_cast<ConstTest*>. Your preferred syntax is not available.

You can either do ConstTest &c = const_cast<ConstTest&>(*this) or ConstTest *c = const_cast<ConstTest*>(this), so pick your favorite.

The result of a const_cast expression is an rvalue unless Type is a
reference type. In this case, the result is an lvalue.

why so and why it is not true in case of pointers?

It is true of pointers. ConstTest* is not a reference type, and the result of const_cast<ConstTest*>(this) is an rvalue. You then assign that value to the variable c.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文