Questions tagged [function-object]

In object-oriented languages, function object (also known as functor) is a feature that allows objects to be used like if they were ordinary functions.

168 questions
0
votes
0 answers

c++ how to call outer class's function from a functor?

I have a functor defined within a class. I want to access member function of the outer class directly from the functor. How do I do that? I want to pass this functor to a template class elsewhere in the code. I dont want to use function…
bsobaid
  • 955
  • 1
  • 16
  • 36
0
votes
2 answers

Declaring and defining a function object inside a class member function

I wonder if and how it is possible to define a function object inside a classes member function to use it directly with, for example, the std::transform function. I know the example is a bit stupid, it's just to show the problem I'm confronted with.…
PiJ
  • 151
  • 1
  • 12
0
votes
0 answers

Instantiating Function Object with unbound Function Objects leads to l-value error

I have the following function objects: /// function object wrapping node and label data into strings of DOT code /// requires as template parameters 2 function objects that define behaviour /// converting node and lable into strings. Requires as…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
0
votes
1 answer

questions about function object in C++

I have a question regarding the following piece of code. template struct DisplayElementKeepCount {     int m_nCount;     DisplayElementKeepCount () { m_nCount = 0; }     void operator () (const T& element){++ m_nCount; cout<
Sarah
  • 453
  • 7
  • 16
0
votes
2 answers

create dynamic functions keeping state

Python beginner question. Say you want to dynamically create a function that keeps some state (in the code below an integer i). Then as the function defined is an object, we could use it later on. In the following code, I add the functions to a…
aaragon
  • 2,314
  • 4
  • 26
  • 60
0
votes
1 answer

Assigning a Function to a Function Object's Property

I have a JavaScript function object defined like this: var minimal = (function() { var handler = undefined; var test = function() { console.log(typeof handler); if (typeof handler === 'function') { handler(); …
Sonny
  • 8,204
  • 7
  • 63
  • 134
0
votes
1 answer

Understanding MyClass x(y); where y is of type MyClass

I have the following code that is part of an exercise (about which I asked here). class MyInt { public: MyInt(int x) : MyValue(new int(x)){}; MyInt() { MyValue = 0; } private: int* MyValue; } int main(int argc,char** argv) { …
Kae
  • 279
  • 2
  • 10
0
votes
1 answer

Should I receive a function by copy, reference or const reference?

When defining a function that takes a function parameter, should I receive a lambda/std::function by copy, reference or const reference? Or all three?
leewz
  • 3,201
  • 1
  • 18
  • 38
0
votes
2 answers

make_heap on std::map with user defined comparison & random access iterator

I have a map defined like this std::map myMap; After processing this map I want to treat it as a heap (based on the second value). I decided to use std::make_heap function.. which is defined like this... template< class RandomIt, class…
vikrant
  • 393
  • 4
  • 15
0
votes
1 answer

Using a function object when the API asks for a function pointer

I want to call a C API from C++. The API expects a function pointer, though for me the functionality is implemented in a C++ function object because it has state. In fact the desired functionaliity is split across into two function objects. The C…
san
  • 4,144
  • 6
  • 32
  • 50
0
votes
2 answers

C++ std:sort() using different criteria

I searched a lot and I am not sure if this is query is repeated but I used this as an reference to create a sort for my std::vector which takes data of following type. typedef struct { int size; int type; int id; } AC; I was able to …
Recker
  • 1,915
  • 25
  • 55
-1
votes
5 answers

for_each usage in C++

#include #include class Abstract { //contains a pure virtual function }; class Mock { public: Mock(); ~Mock() { std::for_each(m_abs_list.begin(), m_abs_list.end(), my_func); } void my_func(Abstract…
Sachin Shetye
  • 378
  • 6
  • 17
-1
votes
1 answer

how to use a function object as a custom comparator for accessing a local variable instead of using a lambda function in C++?

I am trying to learn priority_queue concept in C++, and I came across this interview question. Although, I managed to solve this problem with a lambda function, I could not figure out how to do the same operation with a custom comparator as a…
albin
  • 773
  • 1
  • 8
  • 27
-1
votes
1 answer

the differences between function-object and function-pointer?

i defined a class, then save the pointer to Foo in the priority_queue, and use the cmp-function that i defined. but if the cmp-funtion calls the function-object, an error occurs: class Foo { friend bool cmp(Foo *, Foo *); public: Foo() =…
Wonter
  • 293
  • 1
  • 5
  • 15
-1
votes
3 answers

Python - Pass named arguments to function object

I received a call about stale parameters in a web app. I saw this post... wtforms+flask today's date as a default value ... which was spot on. The field's default was getting set on web server start. It was easy to test. A couple of print…
MrGoodfix
  • 317
  • 2
  • 14
1 2 3
11
12