何时以及如何在 C/C++ 中初始化 const 变量?我对特定类型很好奇:
1) const static member of a class
2) function const local variable
3) const global variable
Run Code Online (Sandbox Code Playgroud)
我的意思当然是应用程序运行时而不是初始化它们的源代码方式。
以下有什么区别:
const char *c = 0; //Here c is a pointer to a constant character
typedef char *pstring;
const pstring cstr = 0 //cstr is a constant pointer to a character
Run Code Online (Sandbox Code Playgroud)
为什么这两个语句有区别,但它们看起来一样。在C++ Primer中提到,第一条语句的基类型是const char,*是声明符的一部分。而对于最后一条语句,基本类型是 const pstring。我没有得到两者之间的区别。
constC++中函数参数的修饰符表示该函数不能改变参数值,但不保证在函数执行过程中不能被其他人改变。因此,编译器无法根据数据不变性进行任何优化。
据我了解,右值引用意味着给定的对象是临时的,因此其他人都无法访问其 data。在这种情况下,编译器可以进行积极的优化吗?
它将允许通过某种方式获得更快的代码
template<class T>
class Immutable
{
private:
const T val;
public:
operator const T && () { return std::move(val); }
};
Run Code Online (Sandbox Code Playgroud)
(只是示例代码),或者const&&当我们确定它们在函数调用期间无法更改时传递值。是否有可能,或者有一些未提及的问题?
c++ optimization constants compiler-optimization rvalue-reference
我在尝试编译项目时遇到了一些问题。它不断给我消息:“候选函数不可行:'this'参数的类型为'const',但方法未标记为const”。以下是出现此错误的函数。
bool operator<(const node& x) const{
if(name < x.name){
return true;
} else{
return false;
}
}
bool operator==(const node& x){
if(name == x.name){
return true;
} else{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人有任何想法或知道我在使用 const 时出了什么问题,我将非常感激。
标题说明了一切。如何更改常数的值?这是否与更改常量数组的索引X处的元素的值相同?
#include<iostream>
int main(){
const char* y = "original";
auto *p = &y;
*p = "modified";
std::cout<<y<<"\n";
//outputs modified
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道一个const值并不意味着要重新分配,但是考虑到 DevTools 是为了在调试时调整值,我认为应该有一种快速的方法来尝试值。让我们说app.js:
const foo = 5;
在控制台中,我尝试在控制台中重新分配它 foo = 6;
显然它不起作用(分配给常量变量)。我也尝试过,delete(window.foo)但即使delete函数返回 true,该值仍然存在。
目前的解决方法是使用本地覆盖(并且必须设置持久文件系统),因为实时编辑不起作用(该值已经存在并且没有本地覆盖,已编辑的值在重新加载后无法保存)。
有没有更简单的方法来快速更改const值?
为什么这段代码不能编译?
class MyClass
{
const int size = 5;
int arr[size];
};
Run Code Online (Sandbox Code Playgroud)
错误说这size是未声明的标识符。
我想定义一个默认常量TIME_NOW并在控制器中我需要的所有地方使用它。我知道的DateTime对象Time.now.utc - 5.hours会给我的东部标准时间,我想定义这个常量作为TIME_NOW在某处ApplicationController。这样做的最佳做法是什么?
你能向我解释一下这种行为吗?这里的代码:
int* b = new int;
const int MAX_AGE = 90;
b = (int*)&MAX_AGE;
std::cout << b << std::endl;
std::cout << &MAX_AGE << std::endl;
std::cout << *b << std::endl;
std::cout << MAX_AGE << std::endl;
std::cout << "........." << std::endl;
*b = 2;
std::cout << *b << std::endl; // HERE I get 2, that's ok
std::cout << MAX_AGE << std::endl; // HERE I still get 90, why?
std::cout << b << std::endl;
std::cout << &MAX_AGE << std::endl;
Run Code Online (Sandbox Code Playgroud) 我正在尝试定义一个常量对象的常量列表,但似乎无法完成。这是我编译良好的示例:
#include <string>
#include <list>
class Person { public:
std::string name;
Person(const std::string &in_name){name=in_name;}
};
class MyClass { public:
const std::list</* const */Person> l;
MyClass(const std::list</* const */Person> &in_l):l(in_l){}
};
int main(int argc, char **argv) {
Person dave("dave");
MyClass c(std::list<const Person>(dave));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我从这const两个地方删除评论时,出现以下错误:
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/string:41,
from main66.cpp:1:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<const Person>’:
/usr/include/c++/7/bits/allocator.h:108:11: required from ‘class std::allocator<const Person>’
main66.cpp:11:53: required from here
/usr/include/c++/7/ext/new_allocator.h:93:7: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) …Run Code Online (Sandbox Code Playgroud) constants ×10
c++ ×8
pointers ×3
arrays ×1
c ×1
class ×1
controller ×1
function ×1
javascript ×1
optimization ×1
ruby ×1
stdlist ×1
variables ×1