C++ 我可以在类定义的头文件中声明一个字符串吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3722775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:33:33  来源:igfitidea点击:

Can I declare a string in a header file in a definition of a class?

c++

提问by Ross

Is it possible to declare a string at all in a header (.h) file, in the definition of a Class?

在类的定义中,是否可以在头文件 (.h) 中声明一个字符串?

When I want to set a default int, I do:

当我想设置默认值时int,我会这样做:

 class MyClass
 {
     static const unsigned int kDATA_IMAGE_WIDTH =  1024;

Is there a way to do the same for the stringobject?

有没有办法对string对象做同样的事情?

 class MyClass
 {
      static const string kDEFAULT_FILE_EXTENSION = "png"; // fail

I know I can use #define...

我知道我可以用#define...

 #define kDEFAULT_FILE_EXTENSION "png"

thanks

谢谢

edit: added that it is in a class definition. updated examples.

编辑:补充说它在类定义中。更新的例子。

回答by Billy ONeal

From the error message you gave (emphasis mine):

从您给出的错误消息(强调我的):

error: invalid in-classinitialization of static data member of non-integral type 'const std::string'

错误:非整数类型“const std::string”的静态数据成员的类内初始化无效

You can do this in a header file, but you cannot do so in a class.

您可以在头文件中执行此操作,但不能在类中执行此操作。

That is:

那是:

class MyClass
{
    static const std::string invalid = "something";
};

is not valid, but

无效,但是

static const std::string valid = "something else";

is valid.

已验证。

If you want the static to be a member of the class only, you do this:

如果您希望静态仅成为类的成员,请执行以下操作:

//Header
class MyClass
{
    static const std::string valid;
};


//Implementation (.cpp) file

const std::string MyClass::valid = "something else again";

Only static const integralclass variables may be initialized using the "= constant" syntax.

只有静态常量整数类变量可以使用“=常量”语法进行初始化。

回答by Ferruccio

yes, but strings are not intrinsic to C++. you need the proper #include

是的,但字符串不是 C++ 固有的。你需要正确的#include

#include <string>

static const std::string kDEFAULT_FILE_EXTENSION = "png";

On a side note. I've never seen C++ code use the k prefix to denote a constant. I think that's an Objective-C convention. Also, ALL_CAPS symbols should really be reserved for #define macros, not language constants.

在旁注。我从未见过 C++ 代码使用 k 前缀来表示常量。我认为这是一个Objective-C约定。此外,ALL_CAPS 符号应该真正保留给 #define 宏,而不是语言常量。

回答by Kevin D.

Yes, you should be able to declare strings or put any other sort of code in your header file. It might be failing because the header file is missing the #include which defines string, or you need to put a "std::" in front of it if you aren't "using namespace std" in the header file.

是的,您应该能够在头文件中声明字符串或放置任何其他类型的代码。它可能会失败,因为头文件缺少定义字符串的 #include,或者如果头文件中没有“使用命名空间 std”,则需要在它前面放置一个“std::”。

Please update your question with the specific compiler error you're seeing if these suggestions don't fix it. Hopefully this helps.

如果这些建议不能解决问题,请使用您看到的特定编译器错误更新您的问题。希望这会有所帮助。

回答by Atul

I don't have the context here, but if the intent is to define an immutable array of characters terminated by a '\0', why not use:

我这里没有上下文,但如果目的是定义一个以 '\0' 结尾的不可变字符数组,为什么不使用:

static const char kDEFAULT_FILE_EXTENSION[] = "png";

static const char kDEFAULT_FILE_EXTENSION[] = "png";

Also, note that there's no transparent conversion between string class and const char * without invoking string::c_str()..

另外,请注意,在不调用 string::c_str() 的情况下,字符串类和 const char * 之间没有透明转换。

回答by Johan Engblom

Two choices:

两种选择:

Define a function in the header that is static and returns the string you want.

在标题中定义一个静态函数并返回您想要的字符串。

static std::string kDEFAULT_FILE_EXTENSION()
    {
        return "png";
    }

A macro will do the right thing here

宏将在这里做正确的事情

#define STATIC_STRING_IN_HEADER(name,value) static std::string name(){return value;}
STATIC_STRING_IN_HEADER(kDEFAULT_FILE_EXTENSION,"png")