Tag: 常量

为什么我可以通过指针转换来改变一个局部的constvariables,而不是C中的全局variables呢?

我想通过使用指针来改变一个常量的值。 考虑下面的代码 int main() { const int const_val = 10; int *ptr_to_const = &const_val; printf("Value of constant is %d",const_val); *ptr_to_const = 20; printf("Value of constant is %d",const_val); return 0; } 如预期的那样,常量的值被修改。 但是当我用全局常量尝试相同的代码时,出现以下运行时错误。 Windows崩溃记者正在开放。 在这个语句中打印第一个printf语句之后,可执行程序正在停止“* ptr_to_const = 20;” 考虑下面的代码 const int const_val = 10; int main() { int *ptr_to_const = &const_val; printf("Value of constant is %d",const_val); […]

在C ++和C中,const int和int const作为函数参数

快速提问: int testfunc1 (const int a) { return a; } int testfunc2 (int const a) { return a; } 这两个function在每个方面都是一样的还是有区别的? 我对C语言的答案感兴趣,但是如果对C ++的情况有兴趣的话,我也想知道。

在ES6类中声明静态常量?

我想在class实现常量,因为这是在代码中find它们的位置。 到目前为止,我一直在用静态方法实现下面的解决方法: class MyClass { static constant1() { return 33; } static constant2() { return 2; } // … } 我知道有可能摆弄原型,但是很多人反对这个。 有没有更好的方法来实现ES6类中的常量?

我应该比常量更喜欢定义?

在C中,我更喜欢常量超过了定义吗? 我最近阅读了很多代码,所有的例子都大量使用了定义。

C ++ – 枚举与常量与#define

在这篇文章的最后: http : //www.learncpp.com/cpp-tutorial/45-enumerated-types/ ,它提到了以下内容: 最后,与常量variables一样,枚举types在debugging器中显示出来, 使得它们在这方面比#defined值更有用 。 上面的大胆的句子是如何实现的? 谢谢。

我可以在PHP类上定义CONST吗?

我在几个类上定义了几个CONST,并想获得它们的列表。 例如: class Profile { const LABEL_FIRST_NAME = "First Name"; const LABEL_LAST_NAME = "Last Name"; const LABEL_COMPANY_NAME = "Company"; } 有什么办法可以获得在Profile类上定义的CONST列表? 据我所知,最接近的选项( get_defined_constants() )不会做的伎俩。 我真正需要的是一个常量名称列表 – 如下所示: array('LABEL_FIRST_NAME', 'LABEL_LAST_NAME', 'LABEL_COMPANY_NAME') 要么: array('Profile::LABEL_FIRST_NAME', 'Profile::LABEL_LAST_NAME', 'Profile::LABEL_COMPANY_NAME') 甚至: array('Profile::LABEL_FIRST_NAME'=>'First Name', 'Profile::LABEL_LAST_NAME'=>'Last Name', 'Profile::LABEL_COMPANY_NAME'=>'Company')

string文字是否为const?

即使在使用大量-Wall -W -pedantic -std=c99选项( -Wall -W -pedantic -std=c99 )时,GCC和Clang也不会抱怨,如果我将string文字分配给char* char *foo = "bar"; 而他们(当然),如果我分配一个const char* char*来做抱怨。 这是否意味着string文字被认为是char*types? 他们不应该是const char*吗? 如果他们被修改,它没有被定义的行为! 和(一个不相关的问题)命令行参数(即: argv ):它被认为是一个string文字数组?

如何在运行时指定path?

实际上,我得到了一个C ++(工作)DLL,我想将其导入到我的C#项目中来调用它的函数。 当我指定DLL的完整path时,它确实有效,如下所示: string str = "C:\\Users\\userName\\AppData\\Local\\myLibFolder\\myDLL.dll"; [DllImport(str, CallingConvention = CallingConvention.Cdecl)] public static extern int DLLFunction(int Number1, int Number2); 问题是这将是一个可安装的项目,所以用户的文件夹将不会是相同的(例如:皮埃尔,保罗,杰克,妈妈,爸爸,…)取决于计算机/会话将在哪里运行。 所以我希望我的代码更通用一些,如下所示: /* goes right to the temp folder of the user "C:\\Users\\userName\\AppData\\Local\\temp" then go to parent folder "C:\\Users\\userName\\AppData\\Local" and finally go to the DLL's folder "C:\\Users\\userName\\AppData\\Local\\temp\\myLibFolder" */ string str = Path.GetTempPath() + "..\\myLibFolder\\myDLL.dll"; [DllImport(str, CallingConvention […]

成员函数结束时的const是什么意思?

C ++中的const关键字在成员函数的末尾(参数列表之后)是什么意思?

我如何从静态上下文获取资源内容?

我想从xml文件中读取string,然后在窗口小部件上执行其他任何类似setText的事情,所以如何在没有活动对象的情况下调用getResources() ?