- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读有关 c++ 中的 const_cast 运算符
1.我无法理解的第一件奇怪的事情是
const_cast 运算符语法即
-const_cast--<--Type-->--(--expression--)--------------------><
我对这种语法的理解是,它有助于摆脱 expression
的常量性。类型 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<ConstTest*>(this)
,我认为 this
的常量指针应该被丢弃,但输出显示它是 this
的对象指失去常量性的。
我感觉代码应该是ConstTest *c = const_cast<ConstTest>(*this)
,但这会产生错误。我知道我在很多解释上都是错误的。请全部改正。
2.我的第二个问题是下面给出的语句
const_cast 表达式的结果是右值,除非 Type 是引用类型。在这种情况下,结果是一个左值。
为什么会这样,为什么在指针的情况下不是这样?
最佳答案
it helps to cast away constness of an expression of type Type
不,Type
是结果的类型,而不是操作数的类型。
What i think is const of this pointer should be casted away
this
类型为 const ConstTest*
. const_cast<ConstTest*>(this)
类型为 ConstTest*
.这就是从指向 const 的指针“抛弃 const”的意思。
I feel code should have been
ConstTest *c =
const_cast<ConstTest>(*this)
const_cast<T>
的结果具有类型 T,这就是它的定义方式。也许你会以不同的方式定义它,但运气不好,你没有得到 ConstTest*
通过写作 const_cast<ConstTest>
, 你可以通过写 const_cast<ConstTest*>
得到它.您的首选语法不可用。
你可以做ConstTest &c = const_cast<ConstTest&>(*this)
或 ConstTest *c = const_cast<ConstTest*>(this)
, 所以选择你最喜欢的。
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?
指针也是如此。 ConstTest*
不是引用类型,const_cast<ConstTest*>(this)
的结果是一个右值。然后将该值赋给变量 c
.
关于c++ - const_cast 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9132315/
我正在阅读 C++ Primer,我发现了一些非常奇怪且难以理解的东西: Record lookup(Account&); //Record and Account are two unrelate
我的问题是,为什么代码的第一部分不起作用而第二部分有效。非常量指针应该修改之前使用 const_cast 的 const 值,但是对于整数这个技巧不起作用。您能解释一下为什么会这样吗? const i
我已经阅读了很多关于 C++ 中的 const_cast 被认为是错误和危险的讨论,除了向后兼容 C 代码之外,不应将其用于任何其他用途。我大体上同意。 但是最近我遇到了以下用例,这让我感到好奇。 我
C++ Primer 一书的第 6.4 章陈述如下: In § 4.11.3 (p. 163) we noted that const_casts are more useful in the con
我看到这个 post这解释了const_cast<>并说使用指针/引用是有益的。但是,请考虑以下代码: 1- const_cast(xadj) 我得到 invalid const_cast from
我正在阅读有关 c++ 中的 const_cast 运算符 1.我无法理解的第一件奇怪的事情是 const_cast 运算符语法即 -const_cast----(--expression--)---
这个问题在这里已经有了答案: const_cast doesn't work c++? [duplicate] (3 个答案) 关闭 6 年前。 在const_cast之后,main函数中的值没有变
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 我有以下代码: int main(){
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 #include using nam
我想使用 C++ 格式进行此转换,它以 C 方式工作。但是当我尝试使用 C++ 格式时它失败了。 有效! void req_password(const void *data, size_t data
我有一个管理输入的类。要显示和更改键绑定(bind),重要的是在完成后将其提交给管理器之前,为调用者提供它可以拥有和更改的绑定(bind)的映射。但是,这个映射中可以插入/删除什么的具体规则只有管理者
我对 const_cast 的返回类型有点困惑?尖括号内的类型是否 <>是返回类型吗? const int i = 5; int b = const_cast(i); 是const_cast返回 in
我有一个函数 static bool Validate(const char& letter, string& aWord) 我需要调用它 Validate(letter, aWord); // wh
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 考虑以下代码: 我声明了一个新的引用端
我有一个大型库,它实现了一些不可变的数据结构。可以想象,其中几乎所有内容都是 const合格的。有一些选择部分不是 const,例如引用计数器。为了处理嵌入结构中的引用计数器,这些引用计数器只能通过
长话短说是否适用于: mapm; m.insert( make_pair( 1, 40 ) ); for( map::iterator it = m.begin(); it !
据我所知,在类中创建常量函数对于读/写编译器优化很有用。 类中的常量函数意味着类成员在函数执行期间将保持不变。但是,您可以通过 const 强制转换隐式参数来绕过此问题(当然,这是一种非常糟糕的做法)
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 考虑以下代码: 我声明了一个新的引用端
考虑以下 C++03 程序: #include struct T { mutable int x; T() : x(0) {} }; void bar(int& x) { x
我正在查看我即将开始使用的 API 的一些示例代码。以下模式让我有点困惑: char* str; str = const_cast("Hello World"); printf("%s ", str)
我是一名优秀的程序员,十分优秀!