我已多次阅读过,在C或C++代码中强制执行const-correctness不仅是一个关于可维护性的好习惯,而且它可能允许编译器执行优化.但是,我也完全相反 - 它根本不会影响性能.
因此,您是否有一些示例,其中const正确性可以帮助您的编译器提高程序的性能?
考虑以下:
class A {
public:
const int c; // must not be modified!
A(int _c)
: c(_c)
{
// Nothing here
}
A(const A& copy)
: c(copy.c)
{
// Nothing here
}
};
int main(int argc, char *argv[])
{
A foo(1337);
vector<A> vec;
vec.push_back(foo); // <-- compile error!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显然,复制构造函数是不够的.我错过了什么?
编辑:Ofc.我无法在operator =()方法中更改this-> c,因此我没有看到如何使用operator =()(尽管std :: vector需要).
想象一下,我有这个C函数(以及头文件中的相应原型)
void clearstring(const char *data) {
char *dst = (char *)data;
*dst = 0;
}
Run Code Online (Sandbox Code Playgroud)
有未定义行为在上面的代码,铸造const
走,或者是它只是一个非常不好的编程习惯?
假设没有使用const限定对象
char name[] = "pmg";
clearstring(name);
Run Code Online (Sandbox Code Playgroud) 我想编写一个函数,输入一个数据数组并使用指针输出另一个数据数组。
我想知道如果两者都指向同一个地址会产生什么结果src
,dst
因为我知道编译器可以针对 const 进行优化。这是未定义的行为吗?(我标记了 C 和 C++,因为我不确定它们之间的答案是否不同,我想了解两者。)
void f(const char *src, char *dst) {
dst[2] = src[0];
dst[1] = src[1];
dst[0] = src[2];
}
int main() {
char s[] = "123";
f(s,s);
printf("%s\n", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
除了上面的问题,如果我删除const
原来的代码,这个定义是否明确?
我一直听到这句话,但我无法真正找到const_cast是邪恶的原因.
在以下示例中:
template <typename T>
void OscillatorToFieldTransformer<T>::setOscillator(const SysOscillatorBase<T> &src)
{
oscillatorSrc = const_cast<SysOscillatorBase<T>*>(&src);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用引用,并使用const,我保护我的引用不被更改.另一方面,如果我不使用const_cast,代码将无法编译.为什么const_cast在这里不好?
这同样适用于以下示例:
template <typename T>
void SysSystemBase<T>::addOscillator(const SysOscillatorBase<T> &src)
{
bool alreadyThere = 0;
for(unsigned long i = 0; i < oscillators.size(); i++)
{
if(&src == oscillators[i])
{
alreadyThere = 1;
break;
}
}
if(!alreadyThere)
{
oscillators.push_back(const_cast<SysOscillatorBase<T>*>(&src));
}
}
Run Code Online (Sandbox Code Playgroud)
请给我一些例子,我可以看到使用const_cast是一个坏主意/不专业.
谢谢你的任何努力:)
是否允许以下内容:
const int const_array[] = { 42 };
int maybe_inc(bool write, int* array) {
if (write) array[0]++;
return array[0];
}
int main() {
return maybe_inc(false, const_cast<int *>(const_array));
}
Run Code Online (Sandbox Code Playgroud)
特别地,它是确定以铸远的常量性const_array
,将其定义为const,只要对象是不实际修改,如在实施例?
当C++类的多个const属性依赖于某些中间计算时,初始化它们的最简单方法是什么?
例如,如何更正下面类的构造函数?
class MyClass {
public:
const int a;
const int b;
MyClass() {
int relatedVariable = rand() % 250;
a = relatedVariable % 100;
b = abs(relatedVariable - 150);
}
};
Run Code Online (Sandbox Code Playgroud) 与帮助const_cast
,如果有一个人是要修改我的声明常量对象又是什么用的const
预选赛?
我的意思是有人如何确保他所宣称的const
不会被修改?
我希望有人能够准确地澄清C++中未定义行为的含义.给定以下类定义:
class Foo
{
public:
explicit Foo(int Value): m_Int(Value) { }
void SetValue(int Value) { m_Int = Value; }
private:
Foo(const Foo& rhs);
const Foo& operator=(const Foo& rhs);
private:
int m_Int;
};
Run Code Online (Sandbox Code Playgroud)
如果我已经正确理解了以下代码中的引用和指针的两个const_casts将删除Foo类型的原始对象的常量,但是通过指针或引用修改此对象的任何尝试都将导致未定义的行为.
int main()
{
const Foo MyConstFoo(0);
Foo& rFoo = const_cast<Foo&>(MyConstFoo);
Foo* pFoo = const_cast<Foo*>(&MyConstFoo);
//MyConstFoo.SetValue(1); //Error as MyConstFoo is const
rFoo.SetValue(2); //Undefined behaviour
pFoo->SetValue(3); //Undefined behaviour
return 0;
}
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是为什么这似乎有效并将修改原始的const对象,但是甚至没有提示我发出警告通知我这种行为是未定义的.从广义上讲,我知道const_casts是不受欢迎的,但是我可以想象一种情况是缺乏对C风格的强制转换可以导致const_cast的意识,而不会被注意到,例如:
Foo& rAnotherFoo = (Foo&)MyConstFoo;
Foo* pAnotherFoo = (Foo*)&MyConstFoo;
rAnotherFoo->SetValue(4);
pAnotherFoo->SetValue(5);
Run Code Online (Sandbox Code Playgroud)
在什么情况下这种行为会导致致命的运行时错误?是否有一些编译器设置可以设置为警告我这种(可能)危险的行为?
注意:我使用的是MSVC2008.
我知道const
应该谨慎地进行抛弃- 并且任何const
从初始const
对象中删除-ness 然后修改对象的尝试都会导致未定义的行为.如果我们想删除const
-ness以便我们可以调用一个不修改对象的非const函数,该怎么办?我知道我们应该实际标记这样的功能const
,但是假设我使用的是一个没有const
可用版本的"坏"代码.
那么,总结一下,下面的代码是"安全的"吗?我的猜测是,只要你没有最终修改对象你就可以了,但我不是百分百肯定.
#include <iostream>
struct Foo
{
void f() // doesn't modify the instance, although is not marked const
{
std::cout << "Foo::f()" << std::endl;
}
};
int main()
{
const Foo foo;
const_cast<Foo&>(foo).f(); // is this safe?
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
const ×5
const-cast ×4
c ×2
casting ×2
constants ×1
constructor ×1
performance ×1
pointers ×1
vector ×1