参数中的字符数组指针与字符串引用

发布于 2024-08-19 19:56:17 字数 256 浏览 4 评论 0原文

我经常看到以下结构,特别是在构造函数中:

class::class(const string &filename)
{
}


class::class(const char * const filename)
{
}

通过逐步调试,我发现如果传递硬编码字符串,则总是调用第二个构造函数。

任何想法:

1)为什么使用对偶结构?

2)速度差是多少?

谢谢。

I often see the following structure, especially in constructors:

class::class(const string &filename)
{
}


class::class(const char * const filename)
{
}

By step-by-step debug, I found out the 2nd constructor is always called if I pass a hard-coded string.

Any idea:

1) Why the dual structure is used?

2) What is the speed difference?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萝莉病 2024-08-26 19:56:17

需要两个构造函数,因为您可以将 NULL 传递给 MyClass::MyClass(const std::string &arg)。提供第二个构造函数可以使您避免愚蠢的崩溃。

例如,您为类编写构造函数,并使其采用 const std::string & ,这样如果您使用 <,则不必检查任何指针是否有效。代码>const char*。
在代码中的任何地方,您都只是使用 std::string。在某些时候,您(或其他程序员)会向那里传递一个const char*。这是 std::string 的精彩部分 - 它有一个构造函数,它接受 char*,这非常好,除了 std: :string a_string(NULL) 编译没有任何问题,只是不起作用。

这就是您所展示的第二个构造函数派上用场的地方:

MyClass::MyClass(const char* arg)
    : m_string(arg ? arg : "")
{}

如果您向它传递一个 NULL ,它将生成一个有效的 std::string 对象。

在这种情况下,我认为您不需要担心任何速度。你可以尝试测量,尽管我担心你会惊讶于差异如此之小(如果有的话)。

编辑:刚刚尝试了 std::string a_string(NULL);,编译得很好,这是在我的机器上运行时发生的情况(OS X + gcc 4.2.1)(我记得我前段时间在 Windows 上尝试过,结果非常相似(如果不完全相同):

std::logic_error: basic_string::_S_construct NULL not valid

Two constructors are needed because you can pass a NULL to your MyClass::MyClass(const std::string &arg). Providing second constructor saves you from a silly crash.

For example, you write constructor for your class, and make it take a const std::string & so that you don't have to check any pointers to be valid if you'd be using const char*.
And everywhere in your code you're just using std::strings. At some point you (or another programmer) pass there a const char*. Here comes nice part of std::string - it has a constructor, which takes char*, and that's very good, apart from the fact, that std::string a_string(NULL) compiles without any problems, just doesn't work.

That's where a second constructor like you've shown comes handy:

MyClass::MyClass(const char* arg)
    : m_string(arg ? arg : "")
{}

and it will make a valid std::string object if you pass it a NULL.

In this case I don't think you'd need to worry about any speed. You could try measuring, although I'm afraid you'd be surprised with how little difference (if any) there would be.

EDIT: Just tried std::string a_string(NULL);, compiles just fine, and here's what happens when it is run on my machine (OS X + gcc 4.2.1) (I do recall I tried it on Windows some time ago, result was very similar if not exactly same):

std::logic_error: basic_string::_S_construct NULL not valid

国产ˉ祖宗 2024-08-26 19:56:17

如果实现本身处理 const char* ,但主要由 std::string 用户调用,则这非常有用。这些可以使用 std::string API 进行调用,该 API 通常只调用 c_str() 并分派到 const char* 实现。另一方面,如果调用者已经有一个 c 字符串,则不需要构造临时或不需要的 std::string(这可能成本高昂,对于较长的字符串,它是堆分配)。

另外,我曾经用它来解决以下情况:

我的接口采用了 std::string,但必须在外部模块中实现,因此模块和模块的 STL 二进制版本调用者模块必须完全匹配,否则就会崩溃(对于可移植库来说不太好......)。因此,我更改了实际接口以使用 const char*,并添加了我声明为内联的 std::string 重载,因此它们不是出口。它没有破坏现有代码,但解决了我所有的模块边界问题。

This is useful if the implementation deals with const char*s by itself, but is mostly called by std::string users. These can call using the std::string API, which usually just calls c_str() and dispatches to the const char* implementation. On the other side, if the caller does already have a c-string, no temporary or unneeded std::string needs to be constructed (which can be costly, for longer strings it's a heap allocation).

Also, I once used it to resolve the following case:

My interface took std::string's, but had to be implemented in an external module, thus the STL binary versions of both the module AND the caller module had to match exactly, or it would have crashed (not really good for a portable library… ). So I changed the actual interface to use const char*, and added std::string overloads which I declared inline, so they weren't exported. It didn't break existing code, but resolved all my module boundary problems.

浪推晚风 2024-08-26 19:56:17

1)为什么采用对偶结构?

如果要方便地将 std::string 对象用作参数,则需要字符串引用版本,因为没有从 std::string 到 const char const 的隐式转换。 const char * const 版本是可选的,因为字符数组可以隐式转换为 std::strings,但它更有效,因为不需要创建临时 std::string。

2) What is the speed difference?

您需要自己测量。

1) Why the dual structure is used?

The string reference version is required if std::string objects are to be used conveniently as parametersm as there is no implicit conversion from a std::string to a const char const. The const char * const version is optional, as character arrays can implicitly be converted into std::strings, but it is more efficient, as no temporary std::string need be created.

2) What is the speed difference?

You will need to measure that yourself.

第七度阳光i 2024-08-26 19:56:17

提供它们基本上是为了方便。有时,如果调用 C 函数,您会得到 char* 指针。其他情况下,您会得到字符串,因此提供两个构造函数只是为调用者提供方便。至于速度,两者实际上具有相同的速度,因为它们都向构造函数发送内存地址。

They are offered basically for convenience. Some times, if you call C functions, you get char* pointers. Others, you get strings, so offering both constructors is just a convenience for the caller. As for the speed, both have virtually the same speed, as they both send a memory address to the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文