标签: const

如何在C#中声明一个常量Guid?

是否有可能在C#中声明一个恒定的Guid?

我明白我可以声明一个static readonly Guid,但是有一种语法允许我写const Guid吗?

c# guid const constants compile-time-constant

51
推荐指数
5
解决办法
4万
查看次数

C中有const吗?

这个问题可能很幼稚,但是:

  • constC中有关键字吗?
  • 从哪个版本?
  • constC和C++ 之间是否存在语义和/或语法差异?

c c++ language-comparisons const

51
推荐指数
5
解决办法
2万
查看次数

为什么有人会使用#define来定义常量?

这是一个简单的问题,但为什么有人会使用#define定义常量?

有什么区别

#define sum 1const int sum = 1;

c++ const c-preprocessor

50
推荐指数
3
解决办法
2万
查看次数

标记所有不修改const的变量是否有任何缺点?

经过大量的谷歌搜索后,我发现了很多关于标记函数及其参数的内容const,但没有关于标记变量的指南const.

这是一个非常简单的例子:

#include <string>
#include <iostream>

void example(const std::string& x) {
  size_t length = x.length();
  for (size_t i = 0; i < length; ++i) {
    std::cout << x.at(i) << std::endl;
  }
}

int main() {
  example("hello");
}
Run Code Online (Sandbox Code Playgroud)

为什么不做

size_t length = x.length();
Run Code Online (Sandbox Code Playgroud)

const喜欢

const size_t length = x.length();
Run Code Online (Sandbox Code Playgroud)

按照惯例?

我知道这么小的一个简单的例子确实没有给这个带来任何巨大的好处,但它似乎对于一个更大的代码库有帮助,你可能会意外地改变你不应该变异的变量.

尽管有这样的好处,但我并没有真正看到它使用那么多(在我见过的C++代码库中)或者提到的几乎和函数及其参数一样多const.

除了必须输入5个额外字符外,还有一些缺点吗?我在这个话题上找不到太多东西,如果这是一个有这么多争议的问题,我不想在脚下射击.

c++ refactoring const

50
推荐指数
4
解决办法
1957
查看次数

49
推荐指数
5
解决办法
8万
查看次数

我可以使用字符串连接在PHP中定义类CONST吗?

我知道你可以使用字符串连接以彼此的方式创建全局常量:

define('FOO', 'foo');
define('BAR', FOO.'bar');  
echo BAR;
Run Code Online (Sandbox Code Playgroud)

将打印'foobar'.

但是,我在使用类常量尝试执行相同操作时遇到错误.

class foobar {
  const foo = 'foo';
  const foo2 = self::foo;
  const bar = self::foo.'bar';
}
Run Code Online (Sandbox Code Playgroud)

foo2的定义没有问题,但声明const bar会出错

解析错误:语法错误,意外'.',期待','或';'

我也尝试过使用像sprintf()这样的函数,但它不像左边的paren那样比字符串连接符'.'.

那么有没有什么方法可以创建类常量,而不是像foo2那样简单的设置案例?

php const class constants

48
推荐指数
3
解决办法
2万
查看次数

在c ++中将非const转换为const

我知道你可以const_cast用来投射const到非const.

但是,如果你想将非const转换const为什么,你应该使用什么?

c++ types casting const constants

48
推荐指数
4
解决办法
6万
查看次数

C++中的声明

根据我的理解,C++中的声明/初始化是具有"基本类型"的语句,后跟逗号分隔的声明符列表.

请考虑以下声明:

int i = 0, *const p = &i; // Legal, the so-called base type is 'int'.
                          // i is an int while p is a const pointer to an int.

int j = 0, const c = 2;   // Error: C++ requires a type specifier for all declarations.
                          // Intention was to declare j as an int and c an as const int.

int *const p1 = nullptr, i1 = 0; // p1 is a const pointer to …
Run Code Online (Sandbox Code Playgroud)

c++ pointers const declaration language-lawyer

48
推荐指数
2
解决办法
2020
查看次数

在标题中用作函数参数的"原始"类型前面删除"const"是否更好?

在代码审查过程中,我的一位同事向我提到,在标题中用作函数参数的"原始类型"前面的"const"是没有意义的,他建议删除这些"const".在这种情况下,他建议仅在源文件中使用"const".原始类型表示诸如"int","char","float"等类型.

以下是示例.

example.h文件

int ProcessScore(const int score);
Run Code Online (Sandbox Code Playgroud)

example.cc

int ProcessScore(const int score) {
  // Do some calculation using score
  return some_value;
}
Run Code Online (Sandbox Code Playgroud)

他的建议如下:

example.h文件

int ProcessScore(int score);  // const is removed here.
Run Code Online (Sandbox Code Playgroud)

example.cc

int ProcessScore(const int score) {
  // Do some calculation using score
  return some_value;
}
Run Code Online (Sandbox Code Playgroud)

但我有点困惑.通常,用户只会查看标题,因此如果标题和源文件之间存在不一致,则可能会导致混淆.

有人可以给出一些建议吗?

c++ const header primitive-types

48
推荐指数
4
解决办法
4875
查看次数

定义局部变量const与类const

如果我使用仅在方法中需要的常量,最好在方法范围内或类范围内声明const吗?是否有更好的性能在方法中声明它?如果这是真的,我认为在类范围(文件顶部)定义它们以更改值并更容易重新编译更为标准.

public class Bob
{
   private const int SomeConst = 100; // declare it here?
   public void MyMethod()
   {
      const int SomeConst = 100; // or declare it here?
      // Do soemthing with SomeConst
   }
}
Run Code Online (Sandbox Code Playgroud)

.net c# jit const

47
推荐指数
3
解决办法
1万
查看次数