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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:53:52+00:00 2026-06-10T12:53:52+00:00

I want to make a class, let’s name it class A, that executes functions

  • 0

I want to make a class, let’s name it class A, that executes functions from other classes. So I want to inherit my classes from this A class and class A to be able to receive function addresses from the derived classes and execute them.

This is what I thought so far:

#include <iostream>
using namespace std;

template <class T>
class A
{
public:
    // a typedef for the function I want to execute 
    // which has no parameters and void as return type
    typedef void (T::*SpecialFunc)();

    A() { }

    //this is the function that executes the received functions
    void exec(SpecialFunc func)
    {
        ((new T)->*func)();
    }
};

class B : public A<B>
{
public:
    B()
    {
        // call A::exec to call my function
        exec(&B::funcB);
    }

    //function I want to be executed
    void funcB()
    {
        cout << "testB\n";
    }
};

int main()
{
    B ob;

    return 0;
}

What I want is function funcB to be called. So far my program breaks with no error, just heavily breaks.
I know that this code can’t work because I try to build class A that needs information from class B and class B needs information from the first class A to be constructed but I hope you understand better what I’m willing to achieve.

Can this be achieved?

Thank you

  • 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-10T12:53:53+00:00Added an answer on June 10, 2026 at 12:53 pm

    Looks like you have infinite recursion here. You call exec() in constructor of B, it creates new B object by new statement which calls constructor of B which calls exec() etc. Probably you need to pass ponter or reference to B object to exec() to avoid its construction.

    And of course B object leak in exec() doesn’t look good. I hope it’s just for testing purposes…

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

Sidebar

Related Questions

So let's say I have two classes that inherit a base class that has
I have style sheet with a class name changebackgroundcolor i want make change in
I have a member variable that in a class that I want to make
Let's say I want to make a HumanBody class. And I want to store
I have a class (let's name it TheClass ) that is quite often used
I've got this class, let's call it Refund (because that's what it's called). I
Let's say I have two classes Foo and Bar, and I want to make
Lets say I want to make a class myClass with two slots A and
I want to make an iostream type class. I would like to find the
I don't want to make a new instance of Form1 in the class, and

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.