C++;:定义简单常数以供使用? 在C++中,我想定义一个常量,可以在另一个函数中使用,一个简短的答案将是怎样的。

C++;:定义简单常数以供使用? 在C++中,我想定义一个常量,可以在另一个函数中使用,一个简短的答案将是怎样的。,c++,string,constants,C++,String,Constants,假设在我的代码开头,我想定义这个常量: //After #includes bool OS = 1; //1 = linux if (OS) { const ??? = "clear"; } else { const ??? = "cls"; } 我不知道用什么类型来定义“清除”字符串。。。我很困惑 稍后,我想在函数中使用它: int foo() { system(::cls); //:: for global return 0; } 如何定义顶部的字符串,如何使用下面的字符串

假设在我的代码开头,我想定义这个常量:

//After #includes
bool OS = 1; //1 = linux
if (OS) {
  const ??? = "clear";
} else {
  const ??? = "cls";
}
我不知道用什么类型来定义“清除”字符串。。。我很困惑

稍后,我想在函数中使用它:

int foo() {
 system(::cls); //:: for global

 return 0;
}

如何定义顶部的字符串,如何使用下面的字符串?我听说char只有一个角色和一些东西。。。我不知道该如何使用,因为它说它正在将字符串转换为常量字符或其他东西。

问题在于,变量超出了范围。如果我在括号内声明某个东西,它只存在于括号内

if( foo ){
    const char* blah = "blah";
}
一旦我们离开
if
语句,变量
blah
就会消失。您需要非本地地将其实例化为您编写的任何括号。因此:

void Bar(){
    const char* blah = "blah";
    if( foo ){
        //blah exists within here
    }
}

但是,
blah
将不存在于
Bar
之外。明白了吗?

char*
不是一个很好的
char
<代码> char */CODE >基本上是一个字符串(它是C++出现之前的字符串)。 举例说明:

int array[N];  // An array of N ints.
char str[N];   // An array of N chars, which is also (loosely) called a string.
char[]
降级为
char*
,因此您经常会看到函数采用
char*

要将
std::string
转换为
const char*
,只需调用:

std::string s;
s.c_str()
在这种情况下,通常使用预处理器来定义操作系统。通过这种方式,您可以使用编译器完成特定于平台的工作:

#ifdef OS_LINUX
const char cls[] = "clear";
#elif OS_WIN
const char cls[] = "cls";
#endif
你可能要考虑的一件事是使它成为一种功能。这避免了对数据的严重依赖

看起来你要做的是:

#include <iostream>
using namespace std;

#ifdef LINUX
const char cls[] = "LINUX_CLEAR";
#elif WIN
const char cls[] = "WIN_CLEAR";
#else
const char cls[] = "OTHER_CLEAR";
#endif

void fake_system(const char* arg) {
  std::cout << "fake_system: " << arg << std::endl;
}

int main(int argc, char** argv) {
  fake_system(cls);
  return 0;
}

// Then build the program passing your OS parameter.
$ g++ -DLINUX clear.cc -o clear
$ ./clear 
fake_system: LINUX_CLEAR
#包括
使用名称空间std;
#ifdef LINUX
const char cls[]=“LINUX_CLEAR”;
#埃利夫温
const char cls[]=“WIN_CLEAR”;
#否则
const char cls[]=“其他清除”;
#恩迪夫
无效伪U系统(常量字符*arg){

std::cout另一个选项是使用一组静态方法创建一个类。为每个命令创建一个新方法。类似于:

// in sys-commands.h
class SystemCommands {
public:
    static char const* clear();
    static char const* remove();
};
这为实现提供了一些很好的选项,最好的选项是为编译时选择的每个平台提供一个单独的实现文件

// in sys-commands-win32.cpp
#include "sys-commands.h"
char const* SystemCommands::clear() { return "cls"; }
char const* SystemCommands::remove() { return "erase /f/q"; }

// in sys-commands-macosx.cpp
#include "sys-commands.h"
char const* SystemCommands::clear() { return "/usr/bin/clear"; }
char const* SystemCommands::remove() { return "/bin/rm -fr"; }
编译哪个文件将决定将使用哪个命令集。您的应用程序代码如下所示:

#include <cstdlib>
#include "sys-commands.h"

int main() {
    std::system(SystemCommands::clear());
    return 0;
}
#包括
#包括“sys commands.h”
int main(){
std::system(SystemCommands::clear());
返回0;
}

编辑:我忘了提到,出于一系列原因,我更喜欢静态函数而不是全局常量。如果没有其他原因,您可以在不更改其类型的情况下使它们成为非常量-换句话说,如果您必须根据运行时设置选择命令集,则用户代码不必更改,甚至不必知道会发生此类更改红色。

您可以使用通用头文件并链接到不同的模块,具体取决于系统:

// systemconstants.hpp

#ifndef SYSTEM_CONSTANTS_HPP_INCLUDED
#define SYSTEM_CONSTANTS_HPP_INCLUDED

namespace constants {
   extern const char cls[];  // declaration of cls with incomplete type
}

#endif
对于Linux,只需编译并链接到以下内容:

// linux/systemconstants.cpp

#include "systemconstants.hpp"

namespace constants {
   extern const char cls[] = "clear";
}
// windows/systemconstants.cpp

#include "systemconstants.hpp"

namespace constants {
   extern const char cls[] = "cls";
}
如果是Windows,只需编译并链接到此窗口:

// linux/systemconstants.cpp

#include "systemconstants.hpp"

namespace constants {
   extern const char cls[] = "clear";
}
// windows/systemconstants.cpp

#include "systemconstants.hpp"

namespace constants {
   extern const char cls[] = "cls";
}

特定于系统的翻译单元可以放置在特定的子目录(linux/、windows/,等等)中,其中一个可以在构建过程中自动选择。这扩展到许多其他方面,不仅仅是字符串常量。

完美,但在我使用的函数中(在while循环中也是如此),我仍然需要调用System(cls),使用::cls编译器失败并说它没有定义,我如何访问范围外定义的cls常量?我认为使用
#ifdef
应该是全局的(函数之外)。对于这种用法,我认为简单的
const char*cls=“clear”
也可以(如果你只想引用一个常量文字,就不需要数组)。@Jason,用一个例子编辑了我的答案。你可以随时查找“向上范围”来查看变量。永远不要向下范围。你的第一个例子中有一个拼写错误,
char str[N};
应该是
char str[N]
。顺便说一句,你应该避免使用这种方法来擦除屏幕:只需在Linux上使用VT转义序列,在Windows上使用控制台API即可。