Questions tagged [non-member-functions]

98 questions
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,727
  • 1
  • 11
  • 24
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
  • 8,896
  • 2
  • 36
  • 87
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
  • 17,157
  • 19
  • 104
  • 189
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
2 answers

How to determine local static variable in non-member function using dwarf

Im trying to use dwarf to compare two c++ files, but I am running into issues when I get to local variables in non-member functions. Consider the following code - int f(){ [static] int j=0; return j; } If I compile it without the static…
0
votes
1 answer

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 a function to get that memeber when passed a…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
0 answers

c++ assignment operator overloading of a non-member function

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; ... } when I overload operators with non-member…
0
votes
0 answers

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.doSomething(); //vs doSomething(foo); If it is as…
Newline
  • 769
  • 3
  • 12
0
votes
0 answers

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, list, etc.) and saw the same situation for them…
0
votes
1 answer

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 declarations and relevant information. I have…
0
votes
1 answer

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 records. Now the sorting can be done on the basis of…
rKA
  • 3
  • 2