Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:45:36+00:00 2026-06-06T06:45:36+00:00

I want to retrieve state from a function object. But the function object has

  • 0

I want to retrieve state from a function object. But the function object has been casted to a function<> template. How can I do it?

I mean:

function object:

class Counter {
private:
    int counter_;
public:
    Counter():counter_(0) {cout << "Constructor() called" << endl;}
    Counter(int a):counter_(a) {cout << "Constructor(a) called" << endl;}

    void operator()(int sum) {
        cout << counter_++ + sum << endl;
    }

int getCounter() { return counter_;}
};

In main. My first step is use object directly:

int main() {
    Counter a(10);
    a(0);
    a(0);

    cout << "Counter: " << a.getCounter() << endl;

Its shows:

Constructor(a) called

10

11

Counter: 12

It’s ok and it’s what I expected.

But in

Counter b(10);
function<void(int)> f = b;
f(0);
f(0);
cout << "Counter: " << b.getCounter() << endl;

It shows

Constructor(a) called

10

11

Counter: 10

Argh!, I supposed that f was a wrapper of real object so modifying f we really modify b. No: f has a copy of b, but I can’t call to f.getCounter() so How can I get State (counter_ var) from f?

I can’t use directly Counter class (in this example) because I have some other similar classes with same signature “void(int)” And I want to use them in indistinctly in a caller function.

I can avoid std::function template at all using a common base class for all my function object but I think there is a solution more C++11 with STL and templates…

So, Is there that solution?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:45:38+00:00Added an answer on June 6, 2026 at 6:45 am

    This is not a cast:

    function<void(int)> f = b;
    

    A std::function is been constructed, with a copy of your function object as its target.

    You can either make the target a reference to your function object:

    function<void(int)> f = std::ref(b);
    

    Or let f contain a copy but then retrieve that copy from f:

    Counter* c = f.target<Counter>();
    cout << "Counter: " << c->getCounter() << endl;
    

    It’s usually safer to let f contain a copy as you don’t ned to worry about the lifetime of b.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML table as follows. I want retrieve row values from this
I want to retrieve website title from webview cookies of my application. I am
Is there any handy functions can retrieve timestamp from given files or even compare
I'll keep it short. I want to obtain the string value from token. But
I apologize if this has been asked before, but I haven't quite found the
I have a problem when trying to retrieve values from an array. I want
Am wondering whether and how i can retrieve a portion of text data from
I want to add some methods to the context retrieved from a canvas object.
I want to know if it is possible to retrieve a row from the
I want to retrieve data from db into a model and pass it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.