我是 GO 的菜鸟,刚刚开始学习这门语言。为什么这样可以:
const name, age = "Kim", 22
但这不是
const name, age := "Kim", 22
在 C 中,这完全可以正常工作:
char* Test = (char*) malloc(sizeof(char));
Test = "Hey";
Run Code Online (Sandbox Code Playgroud)
同时在 Cpp 中它向我抛出了这个错误:
char* Test = (char*)malloc(sizeof(char));
Test = "Hey";
Run Code Online (Sandbox Code Playgroud)
“const char *”类型的值不能分配给“char *”类型的实体
这两者有什么区别,为什么Cpp中的指针总是const并且以后不能修改?
在不依赖的情况下const_cast
,如何const
在之后而不是期间创建一个 C++ 数据成员,当计算多个数据成员需要一个计算成本高昂的中间值时,
下面的最小、完整、可验证的示例进一步解释了这个问题及其原因。为了避免浪费您的时间,我建议您首先阅读示例的两条注释。
\n#include <iostream>\n\nnamespace {\n\n constexpr int initializer {3};\n constexpr int ka {10};\n constexpr int kb {25};\n\n class T {\n private:\n int value;\n const int a_;\n const int b_;\n public:\n T(int n);\n inline int operator()() const { return value; }\n inline int a() const { return a_; }\n inline int b() const { return b_; }\n int &operator--();\n };\n\n T::T(const int n): value {n - 1}, a_ {0}, b_ {0}\n {\n …
Run Code Online (Sandbox Code Playgroud) 我有以下结构:
class Test {
public:
int k = 10;
};
class SecondTest {
private:
Test t;
public:
const Test& myTest() {
return t;
}
};
int main()
{
SecondTest secondTest;
Test tt = secondTest.myTest();
tt.k = 20;
cout << "tt.k value: " << tt.k;
}
Run Code Online (Sandbox Code Playgroud)
我以为:
const Test& myTest() {
Run Code Online (Sandbox Code Playgroud)
将使返回值成为 const。
但不,我可以简单地分配给一个非常量值,并将其用作非常量值:
Test tt = secondTest.myTest();
Run Code Online (Sandbox Code Playgroud)
打印结果将是
"tt.k value: 20"
Run Code Online (Sandbox Code Playgroud)
这对我来说听起来很奇怪......我是否错过了一些概念?
我试图从类方法返回 const 反向迭代器。但是当我将方法标记为 时,我的代码无法编译const
。没有const
代码编译就没有问题。
知道为什么吗?
#include <iostream>
#include <vector>
template <class ValueType>
class DynamicDataComponent {
using ValuesType = std::vector<ValueType>;
using IteratorType = typename std::vector<ValueType>::reverse_iterator;
public:
DynamicDataComponent()
{
values.push_back(0);
values.push_back(1);
values.push_back(2);
values.push_back(3);
}
const IteratorType getRBeginIt() const {
return values.rbegin();
}
private:
ValuesType values;
};
int main() {
DynamicDataComponent<size_t> dataComponent;
std::cout << *dataComponent.getRBeginIt() << "\n";
}
Run Code Online (Sandbox Code Playgroud) 考虑以下两个函数:
void f(int n);
void f(int const n);
Run Code Online (Sandbox Code Playgroud)
从编译器优化的角度来看,后者似乎比前者更好。
然而,从调用者的角度来看,比 .int const n
拥有更多的无用信息和更多的精神负担int n
。
哪个是更好的做法?
c++ optimization performance constants compiler-optimization
const int z = 420;
printf("\n%d | %d",z ,*(&(*(&z+1))-1) );
// O/P:420 | 420
printf("\n%u | %u",&z,(&(*(&z+1))-1) ); //address
// O/P:1310548 | 1310548
*((char *)&z+1) = 21; //I change value for the 1st-Bit
//corrupting constant
printf("\n%d | %d",z ,*(&(*(&z+1))-1) );
//the complex(not really) expression evaluates to z
// O/P:420| 5540
printf("\n%u | %u",&z ,(&(*(&z+1))-1) );
//the complex(not really) expression evaluates to &z
// O/P:1310548 | 1310548
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
似乎我已成功修改了C中的常量
通过修改我的意思是我已经改变了常量地址范围中的位
因为"复杂(不是真的)统一/身份表达"在腐败后改变了价值.
但是z保持不变.为什么?
在解引用时,同一地址如何具有不同的值.?
PS:你可以使用任何身份表达
eg.printf("%d",*(int*)((char*)&(*((char*)&z+1))-1));
Run Code Online (Sandbox Code Playgroud)
[编辑]
好吧让我重新说一下:
z = 420
&z …
Run Code Online (Sandbox Code Playgroud) 我想在C中获取字符串常量的地址.
char * const MYCONST = "StringString";
Run Code Online (Sandbox Code Playgroud)
据我所知,consts被"保存"在内存的文本/代码段中.当我试图获取MYCONSt中第一个元素的地址时:
printf("%p\n",&(MYCONST));
Run Code Online (Sandbox Code Playgroud)
结果我得到0x7fff15342e28,它在堆栈中而不在文本/代码段中.任何人都可以帮我在C中获取字符串常量的地址吗?
//编辑到目前为止我找不到正确的答案:我写的时候
char * const MYCONST1 = "StringString";
printf("Address of MYCONST1: %p\n",MYCONST1);
char * const MYCONST2 = "StringString";
printf("Address of MYCONST2: %p\n",(void*)MYCONST2);
Run Code Online (Sandbox Code Playgroud)
这是输出:
MYCONST1的地址:0x400b91
MYCONST2的地址:0x400b91
但它们应该有不同的地址,因为它们是不同的常量.任何人都可以解释我,结果的长度为7,而不是0x7fffa5dd398c,就像一个locale变量.
谢谢!
请您能帮我解决这个问题吗?
如果在PHP中我有一个定义的常量,可以在变量名中使用该常量名吗?例如,如何在有效的php语法中执行以下操作?
define ("HELLO", "hello-you");
$var_HELLO = "something";
echo $var_HELLO;
Run Code Online (Sandbox Code Playgroud)
其中变量名中的HELLO是定义的常量。
非常感谢。
我经常在我的代码中使用const查找表,它由id和字符串组成.但为了便于阅读,最好使用符号名称(命名常量)而不是id.例:
class LookupTable
{
map<int,string> m { {10,"red"}, {20,"blue"}, {30,"green"} };
enum { col_rd = 10, col_bl = 20, col_gr = 30 };
};
LookupTable tab;
cout << tab.m[10]; // "red", using the id
cout << tab.m[col_bl] // "blue", using a symbol
cout << tab.m[11]; // Typo! Compiler does not check this
cout << tab.m[col_xy]; // Typo! Compiler will complain
Run Code Online (Sandbox Code Playgroud)
在编译时也将检查使用符号名称的拼写错误.
但我喜欢在一个地方定义元素的符号名称,id和字符串,而不是在上部定义值,然后在类声明的下半部分定义命名常量,特别是如果表是相当长.例如,我想写一些像:
mytable.init = { { col_rd, 10, "red" }, // define the symbol names and
{ col_bl, …
Run Code Online (Sandbox Code Playgroud) constants ×10
c++ ×6
c ×3
pointers ×2
c++11 ×1
declaration ×1
go ×1
iterator ×1
methods ×1
optimization ×1
performance ×1
php ×1
string ×1
symbols ×1