Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are helper functions in C++?

Tags:

c++

I was trying to understand what "helper functions" are 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 [note: dead link]. 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?

like image 596
Sumit Gera Avatar asked Oct 09 '13 10:10

Sumit Gera


People also ask

What are the helper functions?

A "helper function" is a function you write because you need that particular functionality in multiple places, and because it makes the code more readable. A good example is an average function. You'd write a function named avg or similar, that takes in a list of numbers, and returns the average value from that list.

What is a helper in programming?

In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).

What are helpers and why do we use them?

A helper is a person who helps another person or group with a job they are doing. The cook and her helpers provided us with refreshment.

What is helper in CI?

In CodeIgniter there are helpers which are there to help you with different tasks. Every helper file is a collection of functions aiming towards a particular role.


2 Answers

"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::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; } 
like image 65
LihO Avatar answered Oct 09 '22 20:10

LihO


There is a great definition of a helper function from the 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 paragraph with a clear example, from which the upper quote is taken.

like image 33
Oleh Holovko Avatar answered Oct 09 '22 20:10

Oleh Holovko