const_cast 的行为
我正在阅读有关 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
指针的const应该被丢弃,但输出显示它是this
的对象code> 指的是失去它的常量。
我觉得代码应该是 ConstTest *c = const_cast
,但这会产生错误。我知道我的很多解释都是错误的。请大家指正。
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 anexpression
of 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否,
Type
是结果的类型,而不是操作数的类型。this
具有类型const ConstTest*
。const_cast(this)
的类型为ConstTest*
。这就是从指向 const 的指针中“抛弃 const”的含义。const_cast
的结果具有类型 T,这就是它的定义方式。也许你会以不同的方式定义它,但运气不好,你不会通过编写const_cast
获得ConstTest*
,而是通过编写const_cast< 获得它;ConstTest*>
。您的首选语法不可用。您可以执行
ConstTest &c = const_cast(*this)
或ConstTest *c = const_cast(this)
,所以选择你最喜欢的。对于指针来说也是如此。
ConstTest*
不是引用类型,并且const_cast(this)
的结果是右值。然后将该值分配给变量c
。No,
Type
is the type of the result, not the type of the operand.this
has typeconst ConstTest*
.const_cast<ConstTest*>(this)
has typeConstTest*
. That's what "casting away const" from a pointer-to-const means.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 aConstTest*
by writingconst_cast<ConstTest>
, you get it by writingconst_cast<ConstTest*>
. Your preferred syntax is not available.You can either do
ConstTest &c = const_cast<ConstTest&>(*this)
orConstTest *c = const_cast<ConstTest*>(this)
, so pick your favorite.It is true of pointers.
ConstTest*
is not a reference type, and the result ofconst_cast<ConstTest*>(this)
is an rvalue. You then assign that value to the variablec
.