C++ 中的辅助函数是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19270178/
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-27 22:39:58  来源:igfitidea点击:

What are helper functions in C++?

c++helper-functions

提问by Sumit Gera

I was trying to understand helper functions in C++ from The C++ Programming Language by Bjarne Stroustrup. But the book hasn't explained anything about it and the purpose of using it in classes. I tried searching for it on Web and found this. I have got the gist of it but still unclear about what is the real purpose of helper functions, when should I use them and on the whole, what are helper functions?

我试图从The C++ Programming Language by Bjarne Stroustrup. 但是这本书没有解释任何关于它的内容以及在课堂上使用它的目的。我试着在网上搜索它并找到了这个。我已经明白了它的要点,但仍然不清楚辅助函数的真正目的是什么,我应该什么时候使用它们,总的来说,辅助函数是什么?

回答by LihO

"helper function"is not a term that you would find in a standard, neither it has an exact definition... standard mentions "helper class"or "helper template"few times to refer to a class, which is not meant to be instantiated by end-users but it provides an useful functionality internally used within another class.

“辅助函数”不是您会在标准中找到的术语,也没有确切的定义......标准多次提到“辅助类”“辅助模板”来指代一个类,这并不意味着由最终用户实例化,但它提供了在另一个类中内部使用的有用功能。

Helper functions are (what I believe the most people mean when they say it) usually functions that wrap some useful functionality that you're going to reuse, most likely over and over again. You can create helper functions meant to be used for many different kinds of purposes...

辅助函数是(我相信大多数人说的意思)通常包含一些有用的功能,您将要重用这些有用的功能,很可能会一遍又一遍地重复使用。您可以创建用于多种不同目的的辅助函数...

An example might be conversion function of any kind, for example function converting multi-byte encoded std::stringto std::wstring:

一个例子可能是任何类型的转换函数,例如函数将多字节编码转换std::stringstd::wstring

std::wstring s2ws(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo( size_needed, 0 );
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

回答by Oleh Holovko

There is a great definition of a helper function from the CppCoreGuidline:

CppCoreGuidline 中有一个很好的辅助函数定义:

A helper function is a function (usually supplied by the writer of a class) that does not need direct access to the representation of the class, yet is seen as part of the useful interface to the class. Placing them in the same namespace as the class makes their relationship to the class obvious and allows them to be found by argument dependent lookup.

辅助函数是一个函数(通常由类的作者提供),不需要直接访问类的表示,但被视为类的有用接口的一部分。将它们放在与类相同的命名空间中,使它们与类的关系变得明显,并允许通过依赖参数的查找来找到它们。

For more info you can check the paragraphwith a clear example, from which the upper quote is taken.

有关更多信息,您可以使用清晰的示例检查段落,从中引用上引号。

回答by Ben10_nf.

An example could be the input validation function that you will be reusing in the entire main function. Let's say you have a program that asks for the user's age, since age is an integer > 0, you'll need to have a separate function that takes care of the "cin >> users_age;". If the input satisfies the condition statement then proceed, otherwise ask the user to re-enter their age.

一个示例可能是您将在整个主函数中重用的输入验证函数。假设您有一个询问用户年龄的程序,因为年龄是一个大于 0 的整数,所以您需要有一个单独的函数来处理“cin >> users_age;”。如果输入满足条件语句,则继续,否则要求用户重新输入他们的年龄。

This is just an example of "helper function". Correct me readers if I'm wrong. Thanks!

这只是“辅助功能”的一个例子。如果我错了,请纠正我的读者。谢谢!

回答by Ben10_nf.

"Helper functions" are described in Bjarne Stroustrups book, and I was just reading about them yesterday. According to Stroustrup good design of a class should keep the number of functions implementing a class to a minimum. You dont want to have 50 functions in a class, according to Stroustrup. Instead you use "helper functions" that use the class interface (call the member functions). They could perhaps (not sure about this) be defined in a shared namespace to give meaning to their "relationship". You can find the paragraph in the book in chapter 9 section 9.7.5

“辅助函数”在 Bjarne Stroustrups 书中有描述,我昨天刚读到它们。根据 Stroustrup 的观点,良好的类设计应该将实现类的函数数量保持在最低限度。根据 Stroustrup 的说法,你不想在一个类中有 50 个函数。相反,您使用使用类接口的“辅助函数”(调用成员函数)。他们可能(对此不确定)在共享命名空间中定义,以赋予他们的“关系”意义。您可以在本书第 9 章第 9.7.5 节中找到该段落