我读过 const 的用例,我觉得我对 const 的大部分都有很好的理解。但是,我似乎无法弄清楚为什么我不经常看到这种情况:
void someFunction(const string& A) const
Run Code Online (Sandbox Code Playgroud)
const 成员函数中有 const 参数。出于某种原因,每当我查找示例并且该函数是 const 时,const 似乎都被从参数中删除,如下所示:
void someFunction(string& A) const
Run Code Online (Sandbox Code Playgroud)
然而,这似乎并没有阻止我修改 A。在 const 成员函数中使用 const 参数是否被认为是不好的形式?
如果 A 不被修改,那么参数中不保留 const 的原因是什么?
编辑:这是我没有澄清的错误,但我理解在参数之前添加它和在函数之后添加它之间的区别。我看过的很多代码都没有将两者结合起来,我只是想弄清楚这是否有原因。
我有与here、here或here相同的问题,除了我为参数和函数设置了const:
#include <unordered_map>
#include <functional>
namespace zzz {
struct identity_t {
static const int ID_SIZE = 5;
static const int LSB = 4; // = ID_SIZE - 1
unsigned char id[ID_SIZE];
inline bool operator< (const identity_t& rhs) const {
for (int i = 0; i < ID_SIZE; i++) if (id[i] != rhs.id[i]) return (id[i] < rhs.id[i]);
return false; // equal
}
unsigned char operator[] (const int& i) {return id[i];}
};
class hash_identity_t {
public:
long …Run Code Online (Sandbox Code Playgroud) 我在 Android 应用程序中有常量变量,如下所示。我想将它们移动到一个单独的常量类,这样它会更干净。你有什么建议?我应该在项目中创建什么类型的文件?我是否只需在 MainActivity 类中导入该常量类?我正在寻找类似于 C++ 中的constants.h 文件。你们有什么建议?
public class MainActivity extends ActionBarActivity {
public static final String NET_SALE = "com.domain.mobile.tipcalculator.MESSAGE1";
public static final String SER_CHRG = "com.domain.mobile.tipcalculator.MESSAGE2";
public static final String TRN_IN = "com.domain.mobile.tipcalculator.MESSAGE3";
public static final String TRN_OUT = "com.domain.mobile.tipcalculator.MESSAGE4";
public static final String POOL = "com.domain.mobile.tipcalculator.MESSAGE5";
public static final String RADIO = "com.domain.mobile.tipcalculator.MESSAGE6";
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
void main ()
{
int a=4;
const int *p=&a;
*p--;
}
Run Code Online (Sandbox Code Playgroud)
在上面的行中,这意味着我们不能通过 p 更改值 a,因此在减量语句中它应该给出错误,但它没有给出错误。谁能解释为什么?
编辑:请注意,正如 @ThomasMatthews 答案中所述,最好不要将数据放入标头中。请参考他的回答。
我想在类的头文件中创建一个 static const char* const 数组。例如:const static char* ar[3] = {"asdf","qwer","ghjk"};但是我收到错误。
这是一个例子:
#include <iostream>
class test{
static const char* const ar[3] = {"asdf","qwer","hjkl"};
}
int main(){}
Run Code Online (Sandbox Code Playgroud)
这是错误:
static data member of type 'const char *const [3] must be initialized out of line
Run Code Online (Sandbox Code Playgroud)
我想知道我想做的事情是否可行。我读过在类定义中定义静态常量整数成员,我从中得到的印象是只能用 int 来做到这一点。如果这是相关的,我使用的是 mac,我的 g++ 版本如下:
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud) 自 GLSL 4.20 起const,限定变量不再需要通过常量表达式进行初始化。但是,当我实际尝试定义const由非常量表达式初始化的全局限定变量时,Mesa 会发出错误。这是示例代码:
#version 420
uniform vec2 v;
const float x=v.x;
out vec4 color;
void main()
{
color=vec4(x,v.y,0,1);
}
Run Code Online (Sandbox Code Playgroud)
以下是我测试编译的方法(以避免任何 OpenGL 代码):
#version 420
uniform vec2 v;
const float x=v.x;
out vec4 color;
void main()
{
color=vec4(x,v.y,0,1);
}
Run Code Online (Sandbox Code Playgroud)
如果我将const float x=v.x;行移到函数体中main,编译将成功结束。
OTOH,nvidia 驱动程序(像往常一样)更加宽容,接受原始代码而不发出警告。
那么,GLSL 4.20+ 实际上是否禁止const在全局范围内使用 -限定变量的非常量表达式初始值设定项,或者这个错误是 Mesa 错误吗?
我想避免在我的 C 文件中硬编码常量值,所以我想知道是否有办法直接在头文件中初始化结构常量,以便在我包含头文件的任何地方使用它?(以 #define 适用于简单类型常量的方式)
到目前为止我找到的所有答案都是:
const int var = 5; /*in header*/
Run Code Online (Sandbox Code Playgroud)
只适用于 C++(不适用于 C)
使用 C 文件来初始化常量,这不是我要找的
这篇文章的最佳答案:如何使用 extern 在源文件之间共享变量?
这似乎有点复杂......
预先感谢您能给我带来的答案或帮助!:)
编辑:我正在为我的问题添加更多详细信息。
我想存储我在结构中使用的系统的硬件参数:
struct parameters_Component1 {
int const1 = 5;
double const2 = 7,847
}
struct parameters_Component2 {
int const1 = 6;
double const2 = 9,3343
}
Run Code Online (Sandbox Code Playgroud)
或等效于的结构
#define myConst 5;
Run Code Online (Sandbox Code Playgroud)
我希望将这些常量值重新分组到一个我可以访问和修改的头文件中,而不是出于组织目的而放在我的 C 代码中
请看,我const int & a = 1通过引用定义,并强制指针转换pa为a,并通过更改 来更改 a 的值pa。这是成功的。
但当const int k = 1定义了 并重复上述操作时,虽然pk和k是相同的地址,* pk但k不是相同的值。
其背后的原理是什么?
您能解释一下当我这样做时IDE如何处理内存分配吗?
const int &a = 1;
int *pa = (int*)&a;
cout << &a << endl;
cout << pa << endl;
*pa = 2;
cout << a << endl;
//And here is the outcome.
//0x7ffeeb5d8a24
//0x7ffeeb5d8a24
//2
Run Code Online (Sandbox Code Playgroud)
这样到这里我们就改a成功了。
const int k = 1;
cout …Run Code Online (Sandbox Code Playgroud) #include <set>\n#include <iostream>\nusing namespace std;\n\ntemplate<class T> \nclass A \n{ \n public:\n A(T a = 0,T b =0): m_a(a),m_b(b) {}\n\n bool operator<(const A& lhs)\n {\n /* compare code */\n }\n \n private:\n T m_a;\n T m_b; \n}; \n\nint main() \n{\n A<int> abc(2,3);\n\n set<A<int>> P2D;\n P2D.insert(abc);\n return 0; \n}\nRun Code Online (Sandbox Code Playgroud)\n当我运行此代码时,出现以下错误
\n\n\n操作数类型为 \xe2\x80\x98const A\xe2\x80\x99 和 \xe2\x80\x98const A\xe2\x80\x99
\n
const如果我在重载上声明关键字operator<:
bool operator<(const A& lhs) const\n{\n /* compare code */\n}\nRun Code Online (Sandbox Code Playgroud)\n它没有给出错误,但是:
\n代码
#include <iostream>
class A
{
public:
mutable int x;
mutable int y;
A(int k1 = 0, int k2 = 0) :x(k1), y(k2) {}
void display()
{
std::cout << x << "," << y << "\n";
}
};
int main()
{
const A a1;
a1.x = 3;
a1.y = 8;
a1.display();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
Error: 'this' argument to member function 'display'
has type 'const A', but function is not marked const
Run Code Online (Sandbox Code Playgroud)
我只是A::display()通过const限定对象调用成员函数a1。那么为什么 line …