互换 const char* 和 std::string
我正在重构一个旧的 C 库,并且当前正在更改外部 API,以便它使用 std::string 而不是 const char*。
ColumnType Table::getColType(const char *name) const
{
int id = getColumnId(name) ;
return getColType(id) ;
}
and
int Table::getColumnId (const char * col_name) const
{
unsigned int i = 0;
while ((i < m_table.num_cols) && (strcmp(m_table.cols[i]->name, col_name) != 0) )
i++;
if (i < m_table.num_cols)
return i;
else
return -1;
}
致:
ColumnType Table::getColType(const std::string& name_in) const
{
const char* name = name_in.c_str();
int id = getColumnId(name) ;
return getColType(id) ;
}
and
int Table::getColumnId (const std::string& col_name_in) const
{
const char* col_name = col_name_in.c_str();
unsigned int i = 0;
while ((i < m_table.num_cols) && (strcmp(m_table.cols[i]->name, col_name) != 0) )
i++;
if (i < m_table.num_cols)
return i;
else
return -1;
}
在新代码中,我将 const char* 传递给现在需要引用 const std::string 的函数。我知道 std::string 可以从 const char* 初始化,并且代码可以正确编译(没有警告等)。
但我只是想确保我不会做任何以后会困扰我的事情(抛开国际问题不谈)。
简而言之——我所做的事情“安全”吗?
I am refactoring an old C library, and am currently changing the external API so that it uses std::string instead of const char*.
ColumnType Table::getColType(const char *name) const
{
int id = getColumnId(name) ;
return getColType(id) ;
}
and
int Table::getColumnId (const char * col_name) const
{
unsigned int i = 0;
while ((i < m_table.num_cols) && (strcmp(m_table.cols[i]->name, col_name) != 0) )
i++;
if (i < m_table.num_cols)
return i;
else
return -1;
}
To:
ColumnType Table::getColType(const std::string& name_in) const
{
const char* name = name_in.c_str();
int id = getColumnId(name) ;
return getColType(id) ;
}
and
int Table::getColumnId (const std::string& col_name_in) const
{
const char* col_name = col_name_in.c_str();
unsigned int i = 0;
while ((i < m_table.num_cols) && (strcmp(m_table.cols[i]->name, col_name) != 0) )
i++;
if (i < m_table.num_cols)
return i;
else
return -1;
}
In the new code, I am passing a const char* to functions that are now expecting a reference to const std::string. I know std::string can be initialised from a const char*, and the code compiles correctly (no warnings etc).
But I just want to make sure that I am not doing anything that will come to bite me later on (I18n issues aside).
In short - is what am doing "safe"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是安全的,只要正确执行,就不会造成任何伤害。
尽管如此,我不认为我会建议继续这样做,除非你能指出一个实质性的好处。
更安全的方法可能是添加一个接受 const
string&
的函数,并直接传递到const char*
函数。这样,您就可以让客户端保留std::string&
代码,而无需修改内部结构。例如:
It's safe, in so far as, executed correctly, it shouldn't cause any harm.
Nevertheless, I don't think I would recommend going forward with this unless you can point to a substantial benefit.
What may be safer is to add a function that takes in the const
string&
, and does a straight pass-thru to theconst char*
function. That way, you're letting clients stay in terms ofstd::string&
code without modifying the internals.For example:
1) getColType 不需要从 std::string 获取 c_str(),只需传递 std::string&直接进入 getColumnId。
2)您应该使用重写的 相等运算符 或使用 std::string::compare 直接代替 strcmp。看
1) No need in getColType to get the c_str() from the std::string, just pass the std::string& directly into getColumnId.
2) You should use the overridden equality operator or use std::string::compare directly instead of strcmp. See
请注意包含 NULL 的 std::string。 C++ 类对他们来说很好; NULL 并不特殊。但 C 字符串当然将其视为字符串结尾。以下不相同:
当您混合搭配时,您必须担心当 C++ 方式为 false 而 C 方式为 true 时会出现奇怪的错误。
Beware of
std::string
s containing NULLs. The C++ class is fine with them; NULL is not special. But C strings of course treat it as end-of-string. The The following are not the same:When you mix and match, you have to worry about weird bugs arising when the C++ way says false, yet the C way says true.