To the excellent responses of eferion and asdasdasd I would like to add some more information.
In general terms, it is considered that the use of free functions (your Option A) is a more flexible and generic solution given that you can take advantage of the function overload , the stl from C ++ take advantage of this with std::begin
and std::end
in the header <iterator>
:
std::vector<int> vi { /* datos */ };
std::list<float> lf { /* datos */ };
std::map<int, int> mii { /* datos */ };
int formacion[100] { /* datos */ };
int formacion2d[10][10] { /* datos */ };
// equivale a vi.begin() el tipo de a es std::vector<int>::iterator
auto a = std::begin(vi);
// equivale a lf.begin() el tipo de b es std::list<float>::iterator
auto b = std::begin(lf);
// equivale a mii.begin() el tipo de c es std::map<int, int>::iterator
auto c = std::begin(mii);
// las formaciones no tienen funciones miembro! el tipo de d es int *
auto d = std::begin(formacion);
// las formaciones no tienen funciones miembro! el tipo de e es int **
auto e = std::begin(formacion2d);
The fundamental types do not have member functions, so it is not possible to obtain an iterator of a 1 formation calling member function begin
, since it lacks it:
int formacion[100] { /* datos */ };
auto a = formacion->begin(); // begin no es miembro de int!
auto b = formacion.begin(); // formacion no es un objeto!
It could be said that the free functions (your Option A) is the way that C ++ has to offer a kind of extension methods such as C #, with the difference that they are not invoked as a member function (your Option B) but as a free function. Precisely that is the focus of the technical document N4165 written by Herb Sutter 2 (my translation):
Motivation
1 Generic code .
It is well known that C ++ has long had the problem of two incompatible function call syntaxes:
-
x.f()
and x->f()
can only be used to call members, such as member functions and callable members; and
-
f(x)
can only be used to call non-members, such as free functions and non-member callable objects.
Unfortunately, this means that the code must know if the function is a member or not. In particular, this syntactic difference defeats the writing of generic code that must select a certain syntax and therefore does not have a reasonable or direct way to invoke the function f
with the object x
without knowing in advance if f
is or is not a member function for that type. Since there is no single syntax that can invoke both, it is difficult or impossible to write generic code that can be adapted.
[...]
2 non-member, non-friend functions increase encapsulation .
"Functions want to be free." Scott Meyers and others have observed and taught that it is good to prefer non-member non-member functions since they naturally increase encapsulation. However, current rules disfavor non-member functions because they are visibly different from callers (they have a different call syntax), and are more difficult to find. This proposal would eliminate the major reasons for following this good guide by making non-member functions as easy to use as the member functions [...].
You can read the rest of the document (in English) in the link that I provided before. There it explains in more detail the pros and cons of the calls to free functions (your Option A) and member functions (your Option B), the technical document N4165 was a proposal for C ++ 17 that did not arrive to be introduced, it proposed to unify the two call syntax to be mutually compatible.
Conclusion.
My opinion is that no option is better than the other, you must choose one or the other based on the needs of your project.
Also known as an array or in English: array.
Prominent C ++ expert, member of the standards committee for more than 10 years and in charge of Visual C ++ at Microsoft since 2002.