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 8450535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:59:38+00:00 2026-06-10T10:59:38+00:00

DISCLAIMER I’m not allowed to use BOOST or any other library, only standard. In

  • 0

DISCLAIMER I’m not allowed to use BOOST or any other library, only standard.

In my class Foo I’ve a template function foo, which takes 2 parameters: a pointer-to-object and a pointer-to-this-object-member-function. The foo function works with pointer of any class, as you can see. I do NOT know, what class will be passed to it. This function creates an instance of a template structure.

template <typename TypeName>
struct Bar
{
    void(TypeName::*action)();
    TypeName* objPtr;
};



template <typename TypeName> void Foo::foo ( void(TypeName::*action)() , TypeName* objPtr )
{
    Bar <TypeName> bar;
    bar.action = action;
    bar.objPtr = objPtr;
};

My question is: how do I store objects of Bar, created in foo, so I can iterate through them later and call the pointer-to-an-object-member-function like this:

(BarStorage[i].objPtr->*BarStorage[i].action)();
  • 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-10T10:59:40+00:00Added an answer on June 10, 2026 at 10:59 am

    Use type erasure:

    class BarBase {
        virtual void invoke() = 0;
    };
    
    template<typename TypeName>
    class Bar final : public BarBase {
        void(TypeName::*action)();
        TypeName* objPtr;
        virtual void invoke() override { (objPtr->*action)(); }
    };
    
    class Foo {
        std::vector<std::unique_ptr<BarBase>> myBars;
    /* ... */
    };
    
    template <typename TypeName>
    void Foo::foo ( void(TypeName::*action)() , TypeName* objPtr)
    {
        auto bar = new Bar<TypeName>();
        bar->action = action;
        bar->objPtr = objPtr;
        myBars.emplace_back(bar);
    };
    

    then simply call myBars[x]->invoke();


    To answer questions in comments.

    What’s up with override? Explicitly say that you override virtual function from a base class. This is not really necessary here, but is considered good practice. For more details see Wikipedia article.

    Its a new feature from C++11, some compilers supported this feature in one way or another for a long time, but its been standardized only now. GCC should support this feature from version 4.7 – you have to use command line parameter -std=c++0x.

    What’s up with unique_ptr? It makes life easier. When you allocate new Bar<XXX>(), you must at some point say delete to free that memory. If you put the pointer in a resource managing object like unique_ptr, you no longer need to worry about deleting it. With vector<BarBase*> you would have to declare a destructor in Foo that does something like:

    for each element in myBars
        delete element
    

    If you use vector<unique_ptr<BarBase>>, you don’t need to worry about deleting anything. When vector is destroyed at end of Foo‘s life, it will destroy its elements – and unique_ptr deletes the memory it contains in its own destructor.

    This is also C++11 feature – addition to standard library. You don’t have to use it (but then make sure to delete everything at the end).

    What’s up with auto? Instead of repeating the same type twice (Bar<Type> * bar = new Bar<Type>();), just use it once and let the compiler deduce the correct type of the variable based on the type of the initializer. It behaves exactly the same, its just less typing and looks better 🙂

    Also C++11 feature, supported in GCC since v4.4.

    Why myBars[x]->invoke() does the right thing? Because invoke is declared virtual in BarBase. This means the method executed is chosen based on the dynamic type (the real type during execution) of myBars[x], not static type. For in-depth explanation, see Wiki. There is a minor run-time overhead with this mechanism, but it can’t be helped when dealing with dynamic types.

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

Sidebar

Related Questions

Disclaimer : I have seen the exact questions, but have not seen any answers
DISCLAIMER: The following code is not something I would ever use in a real
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
Disclaimer: I'm a rails n00b. I'm playing around with a simple helper function that
Disclaimer : I'm quite a novice to RoR, but not with Ruby (if that
(DISCLAIMER: I'm not a programmer, I spend my time on serverfault, I'm just a
Disclaimer: This question is not about fixing visual studio So, I've used VSS for
DISCLAIMER: Don't use ::feof() as your loop condition. For example, see the answer to:
Disclaimer This is not strictly a programming question, but most programmers soon or later
( Disclaimer: This question is not specific to ASP.NET) I have a control which

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.