non-member-functions

Free function versus member function

只谈情不闲聊 提交于 2019-12-18 18:53:42
问题 What is the advantage of having a free function (in anonymous namespace and accessible only in a single source file) and sending all variables as parameters as opposed to having a private class member function free of any parameters and accessing member variables directly? Thanks! header: Class A { int myVariable; void DoSomething() { myVariable = 1; } }; source: namespace { void DoSomething2(int &a) { a = 1; } } int A::SomeFunction() { DoSomething2(myVariable); // calling free function

c++ postfix / prefix operator overload as non-member function

只谈情不闲聊 提交于 2019-12-11 01:45:57
问题 I am writing my own array class as an exercise. Since, I read non-member functions are actually better in some ways than member functions. (Scott Meyers) I am trying to write as many operator overloads as non-member functions as possible. The operator overloads + , - all work out fine as non-member functions. my_array operator+(const my_array & left, const my_array & right); my_array operator-(const my_array & operand); my_array & operator++(); // prefix my_array operator++(int); //postfix,

Static, nonmember or static nonmember function?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 11:08:41
问题 Every time I have some functionality which is in the direction of "utility", I end up wondering which option is the best. For instance, printing message structs (own or external), some encoding/decoding code or simply a few useful conversion functions in the context I'm working. The options I think about are: 1) Static function in helper class/struct. struct helper { static bool doSomething(...); }; 2) Nonmember function. namespace helper { bool doSomething(...); } 3) Static nonmember

Static, nonmember or static nonmember function?

烂漫一生 提交于 2019-12-03 08:04:17
Every time I have some functionality which is in the direction of "utility", I end up wondering which option is the best. For instance, printing message structs (own or external), some encoding/decoding code or simply a few useful conversion functions in the context I'm working. The options I think about are: 1) Static function in helper class/struct. struct helper { static bool doSomething(...); }; 2) Nonmember function. namespace helper { bool doSomething(...); } 3) Static nonmember function. namespace helper { static bool doSomething(...); } In some cases there might be necessary to

Partial template specialization of free functions - best practices

时光怂恿深爱的人放手 提交于 2019-12-03 02:27:29
As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++: template <class T, int N> T mul(const T& x) { return x * N; } template <class T> T mul<T, 0>(const T& x) { return T(0); } // error: function template partial specialization ‘mul<T, 0>’ is not allowed However, partial template specialization of classes/structs is allowed, and can be exploited to mimic the functionality of partial template specialization of free functions. For example, the target objective in the last example can be achieved by using:

Can refactoring an overloaded operator into a non-member function break any code?

北慕城南 提交于 2019-12-01 03:58:34
Consider a legacy class template with overloaded addition operators += and + template<class T> class X { public: X() = default; /* implicict */ X(T v): val(v) {} X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; } X<T> operator+ (X<T> const& rhs) const { return X<T>(*this) += rhs; } private: T val; }; Upon code review, it is observed that + is implementable in terms of += , so why not make it a non-member (and have guaranteed symmetry for left and right arguments)? template<class T> class X { public: X() = default; /* implicit */ X(T v): val(v) {} X<T>& operator+=(X<T> const&

Does C++ have a free function `size(object)`?

北城以北 提交于 2019-11-28 13:22:42
It seems that the way that most people find the size of a string is they just use the my_string.size() and it works fine. Well, I recently did an assignment for class where I did... if (size(my_string) < 5) store[counter].setWeight(stoi(my_string)); Instead of.... if (my_string.size() < 5) store[counter].setWeight(stoi(my_string)); But to my suprise my instructor, who I believe is running an older compiler, wasn't able to run that line of code. On my compiler it works both ways and I'm not quite sure why. A complete program (it outputs 4 for both): #include <string> #include <iostream> using

Can C++ assignment operators be free functions?

大城市里の小女人 提交于 2019-11-28 09:46:15
I'm trying something like this: Foo & operator=(Foo & to, const Bar &from); But I'm getting this error: E2239 'operator =(Foo &, const Bar &)' must be a member function Are there limitations on which operators can/cannot be defined as Free Functions, and if so, why? The assignment operator must be a non-static member function and must have exactly one parameter: An assignment operator shall be implemented by a non-static member function with exactly one parameter (C++03 13.5.3/1). operator() , operator[] , and operator-> must also be implemented as non-static member functions. Class-specific

Does C++ have a free function `size(object)`?

筅森魡賤 提交于 2019-11-27 07:40:29
问题 It seems that the way that most people find the size of a string is they just use the my_string.size() and it works fine. Well, I recently did an assignment for class where I did... if (size(my_string) < 5) store[counter].setWeight(stoi(my_string)); Instead of.... if (my_string.size() < 5) store[counter].setWeight(stoi(my_string)); But to my suprise my instructor, who I believe is running an older compiler, wasn't able to run that line of code. On my compiler it works both ways and I'm not

Can C++ assignment operators be free functions?

给你一囗甜甜゛ 提交于 2019-11-27 03:11:28
问题 I'm trying something like this: Foo & operator=(Foo & to, const Bar &from); But I'm getting this error: E2239 'operator =(Foo &, const Bar &)' must be a member function Are there limitations on which operators can/cannot be defined as Free Functions, and if so, why? 回答1: The assignment operator must be a non-static member function and must have exactly one parameter: An assignment operator shall be implemented by a non-static member function with exactly one parameter (C++03 13.5.3/1).