Questions tagged [non-member-functions]
86 questions
129
votes
2 answers
Operator overloading : member function vs. non-member function?
I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand,…
![](../../users/profiles/438822.webp)
badmaash
- 4,357
- 7
- 40
- 60
39
votes
7 answers
Effective C++ Item 23 Prefer non-member non-friend functions to member functions
While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first…
![](../../users/profiles/308476.webp)
Umut Tabak
- 1,770
- 3
- 24
- 39
30
votes
4 answers
Documenting functions in C++ with Doxygen
I've got a project that I'm using Doxygen to generate documentation for. The documentation of the classes is fine, but I've also got some non-member functions that I use to create objects etc. I'd also like to have these documented, but no matter…
![](../../users/profiles/265249.webp)
Paul
- 3,566
- 6
- 25
- 29
26
votes
3 answers
Free function versus member function
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…
![](../../users/profiles/1012149.webp)
Jana
- 4,806
- 5
- 20
- 28
19
votes
5 answers
Non-member vs member functions in Python
I'm relatively new to Python and struggling to reconcile features of the language with habits I've picked up from my background in C++ and Java.
The latest issue I'm having has to do with encapsulation, specifically an idea best summed up by Item…
![](../../users/profiles/778572.webp)
JimmidyJoo
- 7,935
- 6
- 23
- 27
18
votes
2 answers
Can C++ assignment operators be free functions?
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…
![](../../users/profiles/1737.webp)
Roddy
- 63,052
- 38
- 156
- 264
15
votes
5 answers
Static member functions
After reading sbi and Eli Bendersky's answers in this question I started to wondering what static member functions are for.
A class' friend free function shouldn't be able to do anything a static member function can do? If so, why/when should I…
![](../../users/profiles/300805.webp)
peoro
- 24,051
- 18
- 90
- 141
12
votes
1 answer
Invalid use of 'this' in non-member function
I had working on a class and started writing everything in the same .cpp file. However, after a while I could see the class getting bigger and bigger so I decided to split it into a .h and a .cpp file.
gaussian.h file:
class Gaussian{
private:
…
![](../../users/profiles/889438.webp)
coconut
- 984
- 3
- 10
- 24
12
votes
2 answers
Partial template specialization of free functions - best practices
As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++:
template
T mul(const T& x) { return x * N; }
template
T mul(const T&…
![](../../users/profiles/235825.webp)
Peter Alexander
- 50,304
- 10
- 114
- 163
12
votes
3 answers
Class VERSUS namespace, OR class AND namespace?
Both Class and Namespace?
This question is about a pattern that I am seeing myself use more and more: Having both a class and a namespace for related concepts. I think this is motivated mainly by C++ language artifacts, but not totally.
I suppose…
![](../../users/profiles/1051115.webp)
Krazy Glew
- 6,470
- 1
- 41
- 56
11
votes
3 answers
Does C++ have a free function `size(object)`?
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)
…
![](../../users/profiles/2789574.webp)
Feek
- 287
- 2
- 14
10
votes
3 answers
Static, nonmember or static nonmember function?
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…
![](../../users/profiles/534005.webp)
murrekatt
- 5,525
- 4
- 34
- 61
9
votes
1 answer
Can refactoring an overloaded operator into a non-member function break any code?
Consider a legacy class template with overloaded addition operators += and +
template
class X
{
public:
X() = default;
/* implicict */ X(T v): val(v) {}
X& operator+=(X const& rhs) { val += rhs.val; return *this; }
…
![](../../users/profiles/819272.webp)
TemplateRex
- 65,583
- 16
- 147
- 283
9
votes
2 answers
Multiplying an object with a constant from left side
I have a Matrix class and it has overloaded * operators for scalar and matrix multiplications.
template class Matrix
{
public:
// ...
Matrix operator*(T scalar) const;
// ...
}
// ...
template
![](../../users/profiles/245376.webp)
hkBattousai
- 9,613
- 17
- 66
- 114
8
votes
3 answers
Friend functions of a class template
I have a class template Foo.
I'd like to implement a non-member function Bar that takes two Foos and returns a Foo. I want Bar to be a non-member because it will be more natural for callers to write Bar(f1, f2) than f1.Bar(f2). I also want Bar…
![](../../users/profiles/1386054.webp)
Adrian McCarthy
- 41,073
- 12
- 108
- 157