Questions tagged [non-member-functions]

98 questions
149
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,…
45
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…
33
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…
Jana
  • 5,516
  • 5
  • 23
  • 29
32
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…
Paul
  • 4,239
  • 6
  • 28
  • 29
22
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…
Roddy
  • 66,617
  • 42
  • 165
  • 277
17
votes
6 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…
JimmidyJoo
  • 10,503
  • 7
  • 27
  • 30
15
votes
5 answers

Why do we need static member functions if free functions can do the same?

After reading sbi and Eli Bendersky's answers in this question, I've started to wondering what static member functions are for. Shouldn't a class' friend free function be able to do anything a static member function can do? If so, why/when should I…
peoro
  • 25,562
  • 20
  • 98
  • 150
14
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: …
coconut
  • 1,074
  • 4
  • 12
  • 30
13
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&…
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
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…
Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
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) …
Feek
  • 297
  • 3
  • 15
10
votes
1 answer

unconventional uses of friend in c++

I know the general use cases for the friend keyword with regards to encapsulation but one a couple of occasions, I have needed the friend keyword just to "get the job done". These use cases don't make me happy so I'm wondering if there are some…
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…
murrekatt
  • 5,961
  • 5
  • 39
  • 63
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; } …
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
1
2 3 4 5 6 7