Questions tagged [non-member-functions]
98 questions
2
votes
2 answers
C++ Operator overloading - uni quiz answer looks wrong?
Hi all
There are have been some mistakes on our uni coursenotes this year and I'm just going over quizzes for revision before exams, the attached pic shows the correct answer for "which cannot be implemented as non-member operator…

jewfro
- 253
- 1
- 5
- 15
2
votes
1 answer
c++ postfix / prefix operator overload as non-member function
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…

thassan
- 391
- 3
- 15
2
votes
2 answers
Mocking static functions declared and defined in .cpp without class file using GMOCK
file.h
int func(int);
file.cpp
static int call();
static void print(int x);
int func(int) {
int val = call();
print(val);
}
Here static functions are declared and defined in the same file file.cpp. I have not included definition of static…

Daemon
- 1,575
- 1
- 17
- 37
2
votes
1 answer
How to add method to a class (Helper function)?
How can I add my own method to a pre-existing class without any change in context of pre-existing class.
for example :
A.hpp
class A
{
public :
void print1()
{
cout << "print1";
}
…

Mostafa Sataki
- 305
- 3
- 11
1
vote
3 answers
friend function, cpp
We had an assignment in school implementing a Matrix class that overloads all arithmetic operators. What I did was to (for example) define += as a member function, and then define + as a non-member function that uses += function (both in the same…

yotamoo
- 5,334
- 13
- 49
- 61
1
vote
1 answer
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_;
};
Later I realized it really didn't need any…

jlev
- 21
- 2
1
vote
1 answer
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 ""; }
struct baz final { std::string toString() const { return "baz"; }…

Pam Patrick
- 13
- 4
1
vote
1 answer
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 name lookup.
However, why is it then that in…

glades
- 3,778
- 1
- 12
- 34
1
vote
1 answer
Accessing a C++ non-member function from C# via reflection
I need to gain some run-time information about a C++ program, which is kinda difficult due to C++ not offering some sophisticated reflection mechanism. Now, my approach is to compile the C++ code using /clr and to reflect over the resulting assembly…

Jay
- 237
- 2
- 14
1
vote
0 answers
what operators should be methods , friends, and non-member function?
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 function
but what if we didn't have operator+= , should…

Raea6789
- 141
- 1
- 11
1
vote
1 answer
How to write in-class function from non-class function c++?
I have merge function which is a non-class.
Merge.cpp
template
vector merge(vector left, vector right){
vector result;
int left_current = 0, right_current = 0;
while(left_current < left.size() && right_current <…

ömer feyzullahoğlu
- 13
- 2
1
vote
2 answers
Why member function address are so far away from free functions?
Taking this example: https://godbolt.org/z/gHqCSA
#include
template
std::ostream& operator <<(std::ostream& os, Return(*p)(Args...) ) {
return os << (void*)p;
}
template

Evandro Coan
- 8,560
- 11
- 83
- 144
1
vote
1 answer
Overloading operators with non-member functions
The answer to this question seems to escape me, but how do you go about overloading with non-member functions. Do you just create a program level function and where ever the prototype (or definition) exists the operator is overloaded for that class…

rubixibuc
- 7,111
- 18
- 59
- 98
1
vote
2 answers
How to define an inline free function (non member function) in C++?
In C++, I need to define some inline general functions.
However, when I write the prototype in a header file and the implementation in a.cpp file, I encounter with "LNK2001 unresolved external symbol" error.
Shall I remove the .cpp file and…

Khodaei Ahmad Ali
- 19
- 5
1
vote
1 answer
Helper struct to expose data member to public
so I am trying to construct a class with helper method, namely:
class Type{
int a, b, c;
friend auto helper(auto);
friend auto test_helper(auto);
/* couples test with implement */
public:
void method(){
helper(this);
…

Taylor Huang
- 245
- 2
- 8