我试图更改一个变量的值,该变量定义为int const,如下所示.
const int w = 10;
int* wp = const_cast <int*> (&w);
*wp = 20;
Run Code Online (Sandbox Code Playgroud)
w的值没有改变,即使在赋值之后仍然是10,尽管它表明w和wp都指向同一个内存位置.但是我可以更改w的值,如果在声明时定义如下
int i = 10;
const int w = i;
Run Code Online (Sandbox Code Playgroud)
如果我改变i的声明使其成为常量
const int i = 10;
Run Code Online (Sandbox Code Playgroud)
w的值不会改变.
在第一种情况下,为什么w的值没有改变,即使w和wp指向相同的内存位置[这是我打印地址时的印象]
与编译器有什么不同,它以不同的方式处理这两种情况?
有没有办法确保w不会失去常数,无论它的定义方式如何?
这与当前的MSVC编译器完美编译:
struct Foo
{
} const foo;
Run Code Online (Sandbox Code Playgroud)
但是,它无法使用当前的g ++编译器进行编译:
error: uninitialized const 'foo' [-fpermissive]
note: 'const struct Foo' has no user-provided default constructor
Run Code Online (Sandbox Code Playgroud)
如果我自己提供默认构造函数,它可以工作:
struct Foo
{
Foo() {}
} const foo;
Run Code Online (Sandbox Code Playgroud)
这是MSVC的另一个案例是过于宽松,还是g ++过于严格?
也许标题本身并不清楚......我有一个函数f(由某些库提供),它将一个参数作为签名的函数指针void g(int*),即
void f(void (*g)(int*));
Run Code Online (Sandbox Code Playgroud)
但是,我想使用g带有签名的函数(我定义的)来使用它void g(const int*).先验的,我看不出如何能违反任何常量,正确性,因为所有的签名f说的是,g将只与一个(非叫const)int*(非const),而事实上我可以调用一个void (const int*)函数用不const int*论证.
但海湾合作委员会抱怨并说,
expected 'void (*)(int *)', but argument is of type 'void (*)(const int *)'
Run Code Online (Sandbox Code Playgroud)
我看不出这个投诉是如何合法的,所以有人知道我对此的理解是否错误,或者是否有办法解决?
我知道为什么const在for循环中不起作用.我们需要创建一个新的范围并将值复制到其中.所以这不会飞.
for(const i = 0; i < 5; i++) console.log(i);
Run Code Online (Sandbox Code Playgroud)
虽然这样会.
for(let i = 0; i < 5; i++) console.log(i);
Run Code Online (Sandbox Code Playgroud)
但是,我注意到它们在循环通过像这样的对象的属性时工作.
for(let property in thingy) console.log(property);
for(const property in thingy) console.log(property);
Run Code Online (Sandbox Code Playgroud)
我不知道为什么.
stl充满了这样的定义:
iterator begin ();
const_iterator begin () const;
Run Code Online (Sandbox Code Playgroud)
由于返回值不参与重载分辨率,因此这里唯一的区别就是函数的存在const.这是超载机制的一部分吗?什么是编译器解析线的算法,如:
vector<int>::const_iterator it = myvector.begin();
Run Code Online (Sandbox Code Playgroud) 在分析我的反向传播算法后,我了解到它负责占用我60%的计算时间.在我开始研究并行替代方案之前,我想看看我能做些什么.
该activate(const double input[])功能仅被占用约5%的时间.该gradient(const double input)功能实现如下:
inline double gradient(const double input) { return (1 - (input * input)); }
Run Code Online (Sandbox Code Playgroud)
有问题的培训功能:
void train(const vector<double>& data, const vector<double>& desired, const double learn_rate, const double momentum) {
this->activate(data);
this->calculate_error(desired);
// adjust weights for layers
const auto n_layers = this->config.size();
const auto adjustment = (1 - momentum) * learn_rate;
for (size_t i = 1; i < n_layers; ++i) {
const auto& inputs = i - 1 > 0 …Run Code Online (Sandbox Code Playgroud) 想象一下,我有这个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) 我希望能够做到这样的事情:
class Circle {
const RADIUS_TO_CIRCUMFERENCE = M_PI * 2; // Not allowed
private $radius;
public function __construct( $radius ) {
$this->radius = $radius;
}
...
public function getCircumference() {
return $this->radius * self::RADIUS_TO_CIRCUMFERENCE;
}
}
Run Code Online (Sandbox Code Playgroud)
但我不能从这样的表达式创建一个类常量:
该值必须是常量表达式,而不是(例如)变量,属性,数学运算的结果或函数调用.
所以我的问题是:这种PHP限制的最佳解决方法是什么?我知道以下变通方法,但还有其他更好的方法吗?
class Circle {
private static $RADIUS_TO_CIRCUMFERENCE;
private $radius;
public function __construct( $radius ) {
$this->radius = $radius;
$this->RADIUS_TO_CIRCUMFERENCE = M_PI * 2;
}
...
public function getCircumference() {
return $this->radius * $this->RADIUS_TO_CIRCUMFERENCE;
}
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这个,因为价值$RADIUS_TO_CIRCUMFERENCE可以改变,所以它不是真正的"常数".
我有相当于以下代码:
const int* const n = new int;
printf("input: ");
scanf("%d", n);
delete n;
Run Code Online (Sandbox Code Playgroud)
现在,因为n是指向CONSTANT整数的指针,所以这不应该工作(我期待编译器错误).但是,这似乎可以正常工作,甚至可以将输入值存储到*n中.
我想知道,为什么这不会给我一个错误; 它为什么有效?扫描不能改变*n的值吗?
我需要一种方法来初始化我目前正在处理的程序的数组的const元素.问题是我必须用函数初始化这些元素,没有办法像这样做:
const int array[255] = {1, 1278632, 188, ...};
Run Code Online (Sandbox Code Playgroud)
因为我必须生成很多数据.我尝试的是将数据memcpy到const int,但这不起作用,但没有奏效.
const int array[255];
void generateData(){
for(int i = 0; i < 255; i++) {
initializeSomehowTo(5, array[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望你明白我在想什么,对不起,如果我把这个问题翻倍,我一定忽略了它.
const ×10
c++ ×6
c ×2
constants ×2
arrays ×1
casting ×1
class ×1
ecmascript-6 ×1
g++ ×1
javascript ×1
let ×1
optimization ×1
overloading ×1
php ×1
scanf ×1
scope ×1
visual-c++ ×1