本文介绍了什么是C ++中的辅助函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Bjarne Stroustrup的 C ++编程语言了解C ++中的辅助函数.但是这本书并没有解释任何关于它的内容以及在课堂上使用它的目的.我尝试在Web上搜索它,发现.我已经掌握了要点,但仍不清楚助手功能的真正目的是什么,何时应该使用它们,以及总体上说,什么是助手功能?

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?

推荐答案

辅助功能" 并不是您在标准中会找到的术语,也没有确切的定义...标准多次提及"helper class" "helper template" 来引用一个类,但这并不是由最终用户实例化的但它提供了另一个类内部使用的有用功能.

"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函数(我相信大多数人说的意思)通常是包装一些有用的功能的功能,这些功能将要您重复使用,很可能一遍又一遍.您可以创建旨在用于许多不同目的的辅助函数...

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...

示例可能是任何类型的转换函数,例如将多字节编码的 std :: string 转换为 std :: wstring :

An example might be conversion function of any kind, for example function converting multi-byte encoded std::string to 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;
}

这篇关于什么是C ++中的辅助函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:02