Questions tagged [non-member-functions]
98 questions
3
votes
2 answers
std::visit a std::variant with overloaded free-function instead of function-object
In C++17 is there a simple way to std::visit a variant with an overloaded free-function or must I use an object with an overloaded call operator?
In other words, is it possible to add something simple to make the following //ERROR! line compile to…

Timtro
- 418
- 5
- 15
3
votes
2 answers
Writing a subscript non-member function
I'm guessing this just isn't legal in C++, but I thought I'd ask, given a struct that I don't own:
struct foo {
int x;
int y;
int z;
};
I want to write a non-member subscript operator for it:
int& operator [](foo& lhs, const std::size_t…

Jonathan Mee
- 37,899
- 23
- 129
- 288
3
votes
2 answers
Unit test private methods by making them free functions
In the 2017 cppcon videos, I came across a talk by Klaus Iglberger which was entitled "Free Your Functions!".
In this talk, the speaker talked about how switching to free functions could
easy up the process of testing private methods (See at…

BobMorane
- 3,870
- 3
- 20
- 42
3
votes
2 answers
How to create a handler in Qt?
I'm working on an application using Qt version 4.8 in C++.
I need to use a C function which requires a pointer to a free function:
void handler(int dummy)
I need to change the displayed values in my GUI depending on what happens inside the…

user2738748
- 1,106
- 2
- 19
- 36
3
votes
4 answers
operator overloading and non-member functions c++
I have written a class for complex numbers in which I have overloaded the operator + and everything works fine, however I need to implement this as a non-member function and I am not sure how, or why there is a benefit of doing so.
Here is my code…

user906357
- 4,575
- 7
- 28
- 38
2
votes
3 answers
Non member function can be declared multiple times while member function can only be declared once?
Non mumber function can be delcared multiple times while member function can only be declared once? Is this right ? My example seems saying yes.
But Why ?
class Base{
public:
int foo(int i);
//int foo(int i=10); //error C2535: 'void…

RoundPi
- 5,819
- 7
- 49
- 75
2
votes
2 answers
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 class itself ? Normally I could just put the…
2
votes
2 answers
Error for default parameter value of non-member template friend function
Why does the following code compile with GCC but not with Clang? Who is right and why?
class TF
{
private:
struct S
{
};
template friend void F(T x, S s, int v =…

Meekaa Saangoo
- 309
- 1
- 2
- 8
2
votes
0 answers
GMock EXPECT_CALL failed but test returns OK while mocking C functions
I'm trying to mock the libusb C interface based on the answer here: https://stackoverflow.com/a/41640864/1752391
The tests run just fine if I actually call the expected functions, but when the function call is commented out, the test shows an error…

CsorvaGep
- 33
- 1
- 5
2
votes
0 answers
The "free your functions" approach: could IDEs provide hints?
I have been listening to Klaus Iglberger speech on CppCon 2017 called "Free Your Functions" (you can find it on Youtube here: https://www.youtube.com/watch?v=WLDT1lDOsb4) which exposes better the rationale behind a concept coming from the Effective…

nyarlathotep108
- 5,275
- 2
- 26
- 64
2
votes
0 answers
"Rule of thumb" for free function vs member function
There is a popular guideline (Scott Meyers, Klaus Iglberger, ect) that I recently put more thought into, which is basically prefer non-member (free) functions to member functions. I'm noticing that I can indeed actually pull most of my member…

ricco19
- 713
- 5
- 16
2
votes
3 answers
Doxygen comments for private members and free functions?
Should I use Doxygen comments for private members and free functions? I come from the Java world, and I never added Javadoc for private members. Is this an acceptable practice in the C++ world?

Neo
- 67
- 3
2
votes
1 answer
List of (names of) functions that are specially recognized by C++. (e.g. operator++,begin)
I have just learned C++ for a little bit, and discover some special functions.
Example 1
bool operator<(const B& b1,const B& b2)
bool B::operator<(const B& b2) const
//recognized by std::sort
Example 2
MyIterator C::begin();
MyIterator begin(C&…

javaLover
- 6,347
- 2
- 22
- 67
2
votes
1 answer
IBM Rhapsody: How to use non-member functions in sequence diagrams?
In Rhapsody I have designed an interface which consists of an interface class and a couple of non-member functions. These non-member functions I've put directly into a package which is parallel to the parent package of the interface class. I want to…

J. R.
- 43
- 4
2
votes
3 answers
Pass member OR non-member function pointers as parameters
I have a struct called Foo which contains a function that calls whatever method it is passed and returns the value.
struct Foo
{
unsigned char fooFunc(unsigned char param, unsigned char(getValueMethod)(const unsigned char))
{
return…

Beakie
- 1,948
- 3
- 20
- 46