简体   繁体   English

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

[英]What are helper functions in C++?

I was trying to understand what "helper functions" are in C++ from "The C++ Programming Language" by Bjarne Stroustrup.我试图从 Bjarne Stroustrup 的“C++ 编程语言”中了解 C++ 中的“辅助函数”是什么。 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<\/a> [note: dead link].我尝试在 Web 上搜索它并找到了这个<\/a>[注意:死链接]。 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?我已经掌握了它的要点,但仍然不清楚辅助函数的真正目的是什么,我应该什么时候使用它们,总的来说,什么是辅助函数?

"

"helper function"<\/strong><\/em> is not a term that you would find in a standard, neither it has an exact definition... standard mentions "helper class"<\/em> or "helper template"<\/em> 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. “辅助函数”<\/strong><\/em>不是您在标准中可以找到的术语,也没有确切的定义......标准多次提到“辅助类”<\/em>或“辅助模板”<\/em>来指代一个类,这并不意味着由最终用户实例化,但它提供了在另一个类内部使用的有用功能。

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<\/code> to std::wstring<\/code> :一个示例可能是任何类型的转换函数,例如将多字节编码的std::string<\/code>转换为std::wstring<\/code>的函数:

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;
}

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;".假设你有一个程序询问用户的年龄,因为年龄是一个大于 0 的整数,你需要一个单独的函数来处理“cin >> users_age;”。 If the input satisfies the condition statement then proceed, otherwise ask the user to re-enter their age.如果输入满足条件语句,则继续,否则要求用户重新输入他们的年龄。

This is just an example of "helper function".这只是“辅助功能”的一个例子。 Correct me readers if I'm wrong.如果我错了,请纠正我的读者。 Thanks!谢谢!

"Helper functions" are described in Bjarne Stroustrups book, and I was just reading about them yesterday. Bjarne Stroustrups 书中描述了“帮助函数”,我昨天刚刚读到它们。 According to Stroustrup good design of a class should keep the number of functions implementing a class to a minimum.根据 Stroustrup 的说法,一个好的类设计应该将实现一个类的函数数量保持在最低限度。 You dont want to have 50 functions in a class, according to Stroustrup.根据 Stroustrup 的说法,你不想在一个类中有 50 个函数。 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您可以在本书第 9 章第 9.7.5 节中找到该段落

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM