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

时间:2021-10-30 00:06:30

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?

我试图通过Bjarne Stroustrup的C ++编程语言来理解C ++中的辅助函数。但是这本书没有解释任何关于它的内容以及在课堂上使用它的目的。我尝试在网上搜索它并发现了这一点。我有它的要点,但仍然不清楚辅助函数的真正目的是什么,何时我应该使用它们,总的来说,什么是辅助函数?

4 个解决方案

#1


23  

"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::string to std::wstring:

一个例子可能是任何类型的转换函数,例如将多字节编码的std :: string转换为std :: 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;
}

#2


3  

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

CppCoreGuidline有一个很好的helper函数定义:

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 paragraph with a clear example, from which the upper quote is taken.

有关详细信息,您可以使用一个明确的示例检查该段落,从中获取上部引用。

#3


1  

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.

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

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

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

#4


0  

"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节的书中找到该段落

#1


23  

"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::string to std::wstring:

一个例子可能是任何类型的转换函数,例如将多字节编码的std :: string转换为std :: 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;
}

#2


3  

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

CppCoreGuidline有一个很好的helper函数定义:

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 paragraph with a clear example, from which the upper quote is taken.

有关详细信息,您可以使用一个明确的示例检查该段落,从中获取上部引用。

#3


1  

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.

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

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

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

#4


0  

"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节的书中找到该段落