问题标签 [const-cast]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - Casting Const Pointer To Work With Legacy C Code
As a follow up to my previous question (Variable Length Array Performance Implications (C/C++)), I am having a bit of trouble maintaining const correctness using the C system call writev(). Namely, it appears as though I have run into the exact same problem as this user did in C, although I am using C++:
Here's a snippet of my code:
Given the solution presented from the CodeReview post, I have implemented the following change to the line that's giving me issues since I'd like to avoid the C-style cast:
Is this a valid way of using const cast? As far as I can tell, writev guarentees that the iov structure will not be modified (http://linux.die.net/man/2/writev). This blog post (http://blog.aaronballman.com/2011/06/when-should-you-use-const_cast/) leads me to believe that this is a valid usage (since I am never modifying the buffer), but I'd like to be certain since generally wherever I see const_cast or reinterpret_cast, I get a bit leery.
Thanks in advance for the help.
c++ - 我们可以使用 `const_cast` 来修改常量变量吗?
输出是:
*pia
并且ia
具有相同的地址,但它们具有不同的值。我的目的是用来const_cast
修改一个常量值,但是结果显示它不起作用。
有谁知道为什么?
c++ - 将指针 (T*) 强制转换或转换为两个 const (T const * const) 指针
C++
我想知道一个不是双const
指针的指针(例如T const * const
)是否可以隐式或显式强制转换,通过某些东西(例如函数)处理,或以其他方式转换,以产生 a T const * const
,不使用或在用于初始化之前一个声明为T const * const
. 我怎样才能做到这一点?
我认为如果我从 a 开始T*
,那么一个const_cast
(或两个,以防演员一次只能投射一个const
)就足够了,但显然它没有。T const * const
代码中的许多变量显示了通过强制转换或从函数返回的不同失败尝试。每个演员都未能返回一个带有尾随的指针const
。(我const
将左侧*
称为前导const
,将右侧*
称为尾随const
。)由于强制转换不成功,我尝试强制const
通过直接初始化但未成功。这是在VC11
. stack-crooked.com 上的 g++提供了逻辑上等效的控制台输出,尽管名称不同typeid(/*...*/).name()
.
预期结果与实际结果以及问题:
问题编号对应于相同编号的a#
变量。
- 得到了预期的结果
int *
- 得到了预期的结果
int const *
- 期待
int* const
,但得到了int*
。是否const_cast
忽略了它的尾随const
论点,为什么?由于返回的常量和类型与参数相同,所以演员表运行了吗? - 期待
int const * const
,但得到了int const*
。是否const_cast
忽略了它的尾随const
论点,为什么? - 鉴于参数已经有一个前导,我想看看是否
const_cast<int const * const>
会在结果中包含一个尾随。预期。得到。是否忽略了它的尾随论点,为什么?const
a4
const
int const * const
int const*
const_cast
const
- 预期
int const * const
。得到int const*
。为什么显式演员仍然会排除尾随const
? - 预期
int const * const
。得到int const*
。为什么要static_cast
排除尾随const
? - 预期
int const * const
。得到int const*
。为什么要reinterpret_cast
排除尾随const
? - 预期
int const * const
。得到int const*
。为什么对int const * const
函数返回的初始化仍然会排除const
结果中的尾随? - 预期
int const * const
。从控制台输出获得int const*
,但不是从调试器获得。a10
被明确声明为int const * const
,那么为什么要typeid().name()
排除尾随 const 呢?operator==
yield1
,那么为什么它typeid()
本身(不仅仅是名称)a10
等同于 ofa4
?VC11 调试器将a10
类型列为int const * const
. 为什么它与 fromtypeid()
and不同typeid().name()
?哪一个是正确的? - 变量名
a11
省略,因为它看起来像单词“all”。 - 我预计
a12
是int const * const
因为它被初始化为a10
,这是明确声明int const * const
的。operator==
产量1
,typeid()
仍然如此int const*
。从控制台int const*
输出和调试器中获得。为什么它们与预期的结果不同?
所有强制转换、函数返回和初始化是否都仅限于一次强制转换const
?领导const
是他们唯一可以投入的吗?
c++ - 如何使用 const_cast?
我的Student
类中有一个私有变量,定义为:
我正在尝试为 编写一个复制构造函数,Student
并且我需要抛弃 constness 来执行此操作。不幸的是,我不明白如何使用std::const_cast
.
这就是我在复制构造函数中尝试做的事情:
那不起作用...我不确定语法。
c++ - 测试静态常量成员的存储位置
我有一个
我想测试成员变量b
是否在编译时初始化并因此存储在code-segment
(只读)中。
为此,我尝试更改 的值b
,如果它实际上存储在code-segment
.
我认为应该构建上面的代码,但我的编译器/链接器告诉我undefined reference to 'A::b'
为什么?
c++ - 如何将“指向指针类型的指针”转换为 const?
使用以下代码
我明白了:
我不明白为什么。为什么test
不能默默地投到const double**
?我为什么要明确地这样做?我知道
使我的代码正确,但我觉得这应该是不必要的。
我缺少一些关于const的关键概念吗?
c++ - const_cast 模板参数中 & 的存在是什么意思
我正在查看以下示例
我想知道&
模板参数类型在上面的含义以及为什么没有&
?
更新:
让我重新表述我的问题。我理解它是一个参考,但它指的是什么变量?例如这里它声明它引用原始(未转换)指针的指针。我想知道它在非指针类型的情况下引用了什么,以及b
当它没有被声明为引用时如何引用?
c++ - C++ 中 const_cast 的行为
这是我的问题,问题在评论中
谁能告诉我为什么,推荐一些书籍说明这些问题?谢谢!
c++ - C++ const-cast 引用
这个对吗?它可以用我的编译器编译,但有人告诉我它不能用 AIX 编译。
我应该怎么做才能让我的代码在 AIX 平台上编译?(除了用 Ctrl-C Ctrl-V 重新实现非 const 版本)。
c++ - 将 const char* 转换为 QString
我必须使用类型函数的输出,const char*
并且需要将其转换为QString
.
注意:在该函数中,这些是返回的代码行const char*
_foo
在哪里std::string
。
我尝试使用以下代码行,但总是得到空字符串(实际上不是空的,但只包含换行符并忽略所有其他字符)。