const_cast的行为 [英] behaviour of const_cast

查看:136
本文介绍了const_cast的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于c ++中的const_cast运算符



b
$ b

const_cast运算符语法



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



我对这种语法的理解是,它帮助抛弃了一个表达式的constness 类型。

  > 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->年= 42;
std :: cout<< 这是年<年< std :: endl;
}

这里 ConstTest * c = const_cast< ConstTest *>(this),我认为这个指针的常量应该丢弃,但输出显示它是对象这个是指失去其常数。



我觉得代码应该是 ConstTest * c = const_cast< ConstTest>(* this),但这会产生错误。我知道我错了许多解释。



p> const_cast表达式的结果是右值,除非Type是引用类型。



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

解决方案


它有助于丢弃类型


的表达式的constness


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


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


code> this 有类型 const ConstTest * const_cast< ConstTest *>(this)有类型 ConstTest *


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


code> const_cast< T> 有类型T,这是它的定义。也许你会有不同的定义,但运气不好,你不会得到 ConstTest * const_cast< ConstTest> ,你可以写 const_cast< ConstTest *> 。您的首选语法不可用。



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



< blockquote>

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



为什么这样,为什么在指针的情况下不是真的


这是指针的真实。 ConstTest * 不是引用类型,并且 const_cast (this)的结果是右值。然后将该值赋给变量 c


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?

解决方案

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.

这篇关于const_cast的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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