我习惯在我的C++应用程序中传递这样的字符串:
void foo(const std::string& input)
{
std::cout << input.size() << std::endl;
}
void bar()
{
foo("stackoverflow");
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个我希望字符串为NULL的情况:
void baz()
{
foo("stackoverflow");
foo(NULL); // very bad with foo implementation above
}
Run Code Online (Sandbox Code Playgroud)
我可以foo改为:
void foo(const std::string* input)
{
// TODO: support NULL input
std::cout << input->size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是为了传递一个字符串文字或复制一个char*实现,foo我需要写这样的东西:
void bar()
{
string input("hi"); // annoying temporary
foo(&input);
foo(NULL); // will work as long as foo handles NULL properly
}
Run Code Online (Sandbox Code Playgroud)
我开始考虑继承std::string和添加null属性,但我不确定这是个好主意.也许最好简单地使用一个const char*字符串作为可以为NULL的参数,但是如果我想保存字符串的副本(或NULL)而不必自己管理它的内存呢?(请参阅使用C风格字符串有哪些缺点?等)
任何聪明的解决方案?
jal*_*alf 20
如果希望类型为null,则将其设为指针.传递字符串指针而不是引用,因为这正是指针可以做的,并且引用不能.引用始终指向同一个有效对象.指针可以设置为null,或者重新指向另一个对象.因此,如果你需要指针可以做的事情,使用指针.
或者,使用boost :: optional,它允许更类型安全的方式来指定"此变量可能包含或不包含值".
或者,当然,更改语义,以便您使用空字符串而不是null,传递一个单独的bool参数,指定字符串是否可用,或重构,因此您首先不需要它.
epl*_*ess 11
功能超载救援......
void foo( const std::string& input )
{
std::cout << input << std::endl;
// do more things ...
}
void foo( const char* input )
{
if ( input != NULL ) foo( std::string(input) );
}
Run Code Online (Sandbox Code Playgroud)
这将接受c样式的char数组和std :: strings,并且如果传入字符串文字或char数组,将在堆栈上产生额外的开销,但允许您将实现保留在一个位置并保持良好的语法.
Max*_*ert 10
就个人而言,我会改变语义来传递空的std :: strings而不是NULL:
void foo(const std::string& input)
{
if (!input.empty())
std::cout << input.size() << std::endl;
}
void bar()
{
foo("");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16440 次 |
| 最近记录: |