98 questions
4 votes
1 answer
165 views
Can the standard allow (or would run into contradictions) calling a member function as if it was free function?
A member function pointer must be invoked using the .* (or ->*) syntax, so it can't be passed to a higher-order function: #include <vector> void for_each(auto const& v, auto f) { for (...
2 votes
1 answer
130 views
Should I convert a class with only methods to free functions in a namespace?
I originally created a class like so: class A { public: void run(int x); private: void run_helper1(); void run_helper2(); void run_helper3(); int a_; double b_; bool c_; }; ...
0 votes
1 answer
114 views
What is the minimal way to write a free function to get the member of a class?
(This question popped to my mind after reading this one and its accepted answer.) Assume a class Foo that you cannot modify, that has a public member bar, but no getter for it. You might want to write ...
1 vote
1 answer
706 views
how to call member function if it exists, otherwise free function?
I've got various classes: struct foo final { std::string toString() const { return "foo"; } }; struct bar final { }; std::string toString(const bar&) { return "<bar>"; } ...
2 votes
2 answers
137 views
Call non-member function from inside class, but the nonmember function takes class as input (C++)
As the title suggests, I am wondering if I can call a non-member function (which is contained in the class header file) from inside the class, with the caveat that the nonmember function is using the ...
2 votes
1 answer
710 views
Why is argument-dependent lookup (ADL) choosing class method instead of more fiting free function?
According to cppreference, in Argument-dependent lookups function names are looked up in the namespaces of their arguments in addition to the scopes and namespaces considered by the usual unqualified ...
0 votes
0 answers
27 views
c++ assignment operator overloading of a non-member function [duplicate]
I have a variable class that I am using to make scripting more easier in my program. I am trying to figure out an easy way to do something like the following: myclass { protected: int _data; ... }...
0 votes
0 answers
108 views
Is there a way to make Visual Studio find / offer functions that take a particular type as parameter?
Lets say I implement some functions for type Foo. If they are member functions VS will offer them for a foo object but if I implement them as free / non-member functions then I get no help. foo....
10 votes
1 answer
308 views
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 ...
0 votes
0 answers
81 views
Why are the lexicographical comparison operators assumed as non-member functions for containers?
When having a look at the documentation of std::map, I noticed that the lexicographical comparison operators (between maps) were not counted as member functions. Checked a few other containers (vector,...
1 vote
0 answers
24 views
what operators should be methods , friends, and non-member function? [duplicate]
I have searched for a specific answer for my question and i couldn't find one , so here is my question: I know that if we have operator+ and operator+= += should be a method and + a non-member ...
0 votes
1 answer
1k views
How to declare a function returning a class instance, that is used in the same class?
I've tried couple of weeks and searched for days for an answer, but haven't found it. My code is rather large and intertwined, but my problem is with 3 functions/classes, therefore I will only show my ...
0 votes
1 answer
270 views
Sorting by different data members of a class C++
So, I basically learnt class and also Template functions in C++. Suppose I have a record of students in a class with their roll number, name and total marks. I am using index sorting to sort the ...
-1 votes
1 answer
155 views
Free functions in C++
I want to add v2 to v1. My member function is working, but free function is not. How can I solve this problem, thanks. When I compile with: clang++ -std=c++2a hw1.cpp -o hw1 and run with: ./hw1 give 5 ...
0 votes
1 answer
376 views
C++Linked list non-member function to reverse print
So I understood how to print a single linked list in reverse order using recursion. I'm having trouble with doing it non member functions. For example in int print_reverse(IntSLList & list)) ...