Questions tagged [non-member-functions]

86 questions
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
  • 1
  • 12
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 <…
1
vote
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…
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
user
  • 5,816
  • 7
  • 53
  • 105
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
  • 6,013
  • 18
  • 50
  • 86
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
  • 185
  • 1
  • 7
1
vote
2 answers

How do in include a non-member operator- overloading for a template class in c++?

I am new to c++ and templates is definitely not friendly in syntax. Basically, here are some of the functions i've written, tested, and finished. Just one quick question, i've been trying for hours to write a non-member operator- overload for my…
Nam Vu
  • 1,304
  • 1
  • 8
  • 19
1
vote
2 answers

Algorithm find an element in a container with a given value for one of its members

Something that I have to do quite often is finding a member in a collection of elements which has an element with a given value. For example given: class Person { string getName() const {return mName;} private: string…
1
vote
1 answer

Non member comparison operator for a template class

I have defined a template container Tree, with two member-class iterators : const_iterator and iterator Now I would like to add non member comparison operators: template bool operator==(Tree::const_iterator a, Tree::iterator…
galinette
  • 7,938
  • 29
  • 73
1
vote
1 answer

In operator lookup no preference is given to members over nonmembers

Stroustrup writes : Consider a binary operator @. If x is of type X and y is of type Y, x@y is resolved like this: • If X is a class, look for operator@ as a member of X or as a member of a base of X; and • look for declarations of operator@ in the…
1
vote
3 answers

Non-friend, non-member functions increase encapsulation?

In the article How Non-Member Functions Improve Encapsulation, Scott Meyers argues that there is no way to prevent non-member functions from "happening". Syntax Issues If you're like many people with whom I've discussed this issue, you're likely…
Magnus W
  • 11,902
  • 10
  • 61
  • 135
1
vote
1 answer

Overloading the multiplication operator in c++

I've written a C++ interface to LAPACK, but I'm running into some memory issues that have made me reconsider some of operator overloading. Right now, I have overloaded the operator* outside of the the class definition (but as a friend to the Matrix…
1
vote
0 answers

Non-friend, non-member function accessing private data member

I am trying to use the istream function below to access the private data members numerator and denominator, however, I am getting errors about it being private. istream is a non-friend, non-member function (I cannot make it a friend). I understand…
1
vote
1 answer

operator overloading - inline non-member functions

OK, so I can get my code to work, but there's something that's bugging me. It has to do with operator overloading and making non-member functions inline. Here's a very simple program that implements a complex number object: Contained in…
1
vote
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
  • 185
  • 1
  • 11