如何用C++中的默认参数初始化"unsigned char*"?

RSF*_*on7 3 c++ initialization default-arguments

我有一个带有以下签名的方法的类:

void print(unsigned char *word);
Run Code Online (Sandbox Code Playgroud)

我需要设置""为默认值word,我该怎么做?

我尝试了显而易见void print(unsigned char *word="");但我得到以下错误:

error: cannot initialize a parameter of type
  'unsigned char *' with an lvalue of type 'const char [1]'
    void print(unsigned char *word="");
Run Code Online (Sandbox Code Playgroud)

因为我不能word用字符串文字初始化我该怎么办?

Lig*_*ica 7

你说这是一个适用于打印的"前缀"参数.

答案是你应该制作参数const,停止在函数内做你正在做的任何突变,然后""用作默认参数:

void print(const char* prefix = "")
Run Code Online (Sandbox Code Playgroud)


And*_*rsK 6

尝试

unsigned char empty[] = { 0 };

void print(unsigned char* word = empty )
{
  ...
}
Run Code Online (Sandbox Code Playgroud)