具有“const”的C型铸造关键词
我通常在 C/C++ 代码中使用 C 类型转换。我的问题是,在转换类型中添加“const”关键字对结果意味着什么吗?
例如,我可以想到几种场景:
const my_struct *func1()
{
my_struct *my_ptr = new my_struct;
// modify member variables
return (const my_struct *)my_ptr;
// return my_instance;
}
在这种情况下,函数构造一个结构体的新实例,并将其转换为常量指针,因此调用者将无法进一步修改其内部状态,除非删除它。 “const”转换是必需的、推荐的还是根本不必要的,因为任一 return 语句都有效。
在本例中,my_base
是 my_derive
的基类。
const my_base *func2(const my_derive *my_ptr)
{
return (const my_base *)my_ptr;
// return (my_base *)my_ptr;
}
由于 my_ptr
已经是一个 const 指针,那么使用 (my_base *)
对其进行强制转换是否会涉及用于删除 const 的 const_cast 以及返回时另一个隐式 const_cast ?
是否有任何理由将“const”添加到整数函数参数中,因为更改它永远不会影响函数外部的状态?
void func3(const int i)
{
// i = 0; is not allowed, but why, as it is harmless?
}
在转换整数时添加“const”怎么样?我认为这应该类似于 func2() 。
void func4(short i)
{
const unsigned int j = (const unsigned int) i;
// const unsigned int j = (unsigned int) i;
}
如果我错了请纠正我。考虑到类型转换可能是一个常见问题解答,我不确定这个是否与其他内容重复。谢谢!
I usually use C type casting in C/C++ code. My question is, does adding the "const" keyword in the casting type mean anything to the result?
For example, I can think up several scenarios:
const my_struct *func1()
{
my_struct *my_ptr = new my_struct;
// modify member variables
return (const my_struct *)my_ptr;
// return my_instance;
}
In this one, the function constructs a new instance of a struct, and casting it to to a constant pointer, therefore caller will not be able to further modify its internal state except deleting it. Is the "const" casting required, recommended, or simply unnecessary, since either return statement works.
In this one, my_base
is the base class of my_derive
.
const my_base *func2(const my_derive *my_ptr)
{
return (const my_base *)my_ptr;
// return (my_base *)my_ptr;
}
Since my_ptr
is already a const pointer, would casting it with (my_base *)
involve a const_cast for removing const and another implicit const_cast when returning?
Is there any reason to add "const" to an integer function argument, since changing it never affect state outside the function?
void func3(const int i)
{
// i = 0; is not allowed, but why, as it is harmless?
}
How about adding "const" when casting an integer? I think this should resemble func2()
.
void func4(short i)
{
const unsigned int j = (const unsigned int) i;
// const unsigned int j = (unsigned int) i;
}
Correct me if I'm wrong. Considering type casting might be an FAQ, I'm not sure if this one duplicates with anything else. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在转换类型中添加 const 关键字意味着结果将是常量。以下内容不会在 C++ 中编译(在 C 中没有效果):
您确实不应该在 C++ 代码中使用 C 类型转换。它不安全,只能用于与遗留 C 代码兼容。您应该使用 C++ 强制转换。
在
func3
中的情况下,通常不使用const
限定符。如果函数参数没有指针或引用类型,则没有什么大理由向函数参数添加 const 限定符。请考虑以下事项:将左值分配给右值时(如
func4
中所示),无需在强制转换表达式中显式指定const
限定符。左值到右值的转换将根据 C++ 标准 4.1 隐式执行。Adding the
const
keyword in the casting type means that the result will be constant. The following will not compile in C++ (in C it has no effect):You really shouldn't use C type casting in your C++ code. It is not safe and should be used only for compatibility with the legacy C code. You should use C++ casts instead.
In cases as in
func3
usuallyconst
qualifier is not used. There's no big reason to addconst
qualifier to function argument if it has not pointer or not reference type. Consider the following:When you assign lvalue to rvalue, as in
func4
, there's no need to explicitly specify theconst
qualifier in the cast expression. Lvalue-to-rvalue conversion will be performed implicitly according to the C++ Standard 4.1.将
const
添加到强制转换就像将const
添加到任何其他类型说明符一样 — 结果对象是const
。这意味着什么取决于上下文。如果您在顶层添加const
(例如const int
或const foo
或int* const
,那么您在大多数情况下,它可以像非 const 对象一样被复制和分配(尽管有一些例外,例如 std: :auto_ptr)。如果将
const
添加到指针,则也可以将其添加到指向的类型,例如int * const
是一个 <。 code>const 指向普通int
的指针,而const int*
或int const*
是指向int
的普通指针>const int位于参数声明顶层的
const
就像声明任何其他局部变量const
--- 你不能修改其中的命名对象。然而,这个 const 并不构成函数类型的一部分,因此您可以在没有它的情况下前向声明该函数(至少对于符合要求的编译器来说)这可以说是一种很好的风格。 --- 变量是否在函数内被视为 const 是一个实现细节,因此不应出现在原型中。在定义中,如果您不打算修改某些内容,则它遵循声明
const
的准则。在参数声明的其他地方(例如作为指针的一部分)使用 const 确实会影响函数类型,就像强制转换一样。
Adding
const
to a cast is just like addingconst
to any other type specifier --- the resulting object isconst
. what this means depends on the context. If you addconst
at the top level (e.g.const int
orconst foo
orint* const
, then you just have aconst
object. In most cases this can be copied and assigned just like a non-const
object can (though there's a few exceptions, likestd::auto_ptr
).If you add the
const
to a pointer then you can also add it to the pointed-to type. e.g.int * const
is aconst
pointer to a plainint
, whereasconst int*
orint const*
is a plain pointer to aconst int
.const
at the top level in a parameter declaration is just like declaring any other local variableconst
--- you cannot modify the named object within the function body. However, thisconst
does not form part of the function type, so you can forward-declare the function without it (with conforming compilers at least).This could be argued to be good style --- whether or not the variable is treated as
const
inside the function is an implementation detail so shouldn't be in the prototype. Within the definition, it follows the guideline of declaring somethingconst
if you don't intend to modify it.Using
const
elsewhere in the parameter declaration (e.g. as part of a pointer) does affect the function type, as with the casts.在第一个示例中,在需要 const 的函数中返回非 const 会再次捕获它,这意味着 const 很重要,但您无法注意到它,因为如果它不存在,则会添加它。
在第二个示例中,它将是一个维护 const 的强制转换。
在第三个示例中,它是不允许的,因为它是无害的:我将更改函数的其余部分并且不可恢复。
请记住,将类型放入容器中而不是它的类型将自动转换它,就像放入长字段中的 int 不会保持 int 一样。
您还必须记住,对于汇编器来说,const 的含义与 private 或 public 大致相同,这没什么。无论您将 const 放在哪里,生成的汇编代码都不会有所不同,并且将以完全相同的速度运行。
In the first example returning non-const in a function that requires const will cat it again meaning that const matters, but you can't notice it because if it's not there it will be added.
In the second example it will be one cast maintaining const.
In third example it is disallowed because it is harmless: i will change for the rest of the function and unrecoverable.
Keep in mind that putting type into container not of it's type will cast it automaticllay same as int put in long field won't remain int.
You also have to remember that const means about the same as private or public to the assembler, which is nothing. The assembler code generated will not differ at all no matter where you put const and will run at same speed exacly.