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

  • Home
  • SEARCH
  • 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 7831275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:39:12+00:00 2026-06-02T11:39:12+00:00

Here is my code & requirements: I have 2 classes A & B, they

  • 0

Here is my code & requirements:

I have 2 classes A & B, they don’t have any inheritance. I used one method of class A to call one method of Class B. Then the class B’s method should callback one of the class A’s method to execute a callback.

Part of code:

classA.h:

#include "classB.h"

class classA
{
  public:
  classA();
  classB *pClassB;
  void callClassB();
  void callBack();
};

classA.cpp:

#include "classA.h"
classA::classA()
{
  pClassB = new classB();
}

void classA::callBack()
{
  return;
}

void classA::callClassB()
{
  pClassB->callFunction();
}

classB.h:

class classB
{
  public:
  classB();
  void callFunction();
}

classB.cpp:

#include "classB.h"
classB::classB()
{
}

void classB::callFunction()
{
   // I should call classA's callback here!
}

The problem is, I can’t include classA.h in classB.h because it will cause some compile issue elsewhere(I can’t solve that). I can’t make classB as classA’s subclass(if I can, I just have to do classA::callBack() instead). So is there a solution to this situation?

UPDATE:that’s what I’ve modified:

class classB
{
  public:
  classB(classA& pCallBack);
  void callFunction();
  void (classA::*m_callback)(void);
};

classA::classA()
{
  pClassB = new classB(*this);
}

classB::classB(classA& pCallBack)
{
  m_callback = pCallBack;
}

I tried to save the pointer, but also failed. It says “assigning from incompatible type”… what’s wrong with it??

  • 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-02T11:39:14+00:00Added an answer on June 2, 2026 at 11:39 am

    Indeed, reciprocally including both headers in each other is a circular dependency, which the compiler can’t resolve. There are different ways to solve this, three of which I describe below.

    Using forward declarations

    The most straightforward way to break the circular dependency is to use forward declarations. A forward declaration only tells the compiler that a given name denotes a class. That is enough if you only use pointers/references to that type, because in these cases the compiler doesn’t need the full definition of that type. So you can change classA.h like this:

    class classB;
    
    class classA
    {
      public:
      classA();
      classB *pClassB;
      void callClassB();
      void callBack();
    };
    

    This way the header is not dependent on classB.h anymore. Of course, you need now to #include that header in classA.cpp:

    #include "classA.h"
    #include "classB.h"
    ...
    

    Introducing an interface

    Another way is to introduce a super interface (or in C++, an abstract superclass) to classA, and let classB see only that:

    callback.h:

    class callback
    {
      public:
      virtual void callBack() = 0;
    };
    

    classA.h:

    #include "callback.h"
    
    class classB;
    
    class classA : public callback
    {
      public:
      classA();
      classB *pClassB;
      void callClassB();
      void callBack();
    };
    

    classB.h:

    #include "callback.h"
    
    class classB
    {
      callback& m_a;
    public:
      classB(callback& a);
      void callFunction();
    };
    

    classB.cpp:

    #include "classB.h"
    
    classB::classB(callback& a) : m_a(a) {}
    
    classB::callFunction()
    {
      m_a.callBack();
    }
    

    As you see, this way classB does not depend on classA in any way. This in fact allows you to later replace classA with any other implementation of callback, without touching classB‘s definition.

    Introducing a function pointer

    Yet another possibility would be to define / use a function pointer type matching that of classA::callBack(), and pass only the pointer to the callback function to classB, rather than the whole classA object. Although, this only works seamlessly with static methods – for nonstatic functions you still need a classA object for the call.

    Update

    You are mixing two approaches (passing an object of classA or passing only a function pointer) in your modified code. Better stick with one at a time. The first approach is simpler, this is how it would look like:

    class classA;
    
    class classB
    {
      classA& m_a;
    public:
      classB(classA& a);
      void callFunction();
    };
    
    classB::classB(classA& a) : m_a(a) {}
    
    classB::callFunction()
    {
      m_a.callBack();
    }
    

    The function pointer approach would be more complex for nonstatic member functions, because you needed both the function pointer and an object on which to invoke it. An alternative take on it is shown in @Tadeusz’s answer.

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

Sidebar

Related Questions

Here is my code: class Soldier { public: Soldier(const string &name, const Gun &gun);
Here is the input form code <tr valign=top> <td align=right class=innertablestyle><font class=normal><strong>Homepage</strong></font></td> <td>&nbsp;</td> <td>
Here is my code that fails: bool Table::win(const Card &card) { for (int i
Here's some code: DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = (&(objectClass=user)(sAMAccountName= + lstUsers.SelectedItem.Text +
Here's the code: NSError *parseError; NSMutableArray *listOfObjects = [NSJSONSerialization JSONObjectWithData:[@[] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&parseError]; NSLog(@Is
Here's the line from App.Config: <add key=CheckFileFormatString value=P{0}\t&quot;{1}, {2}&quot;\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}/> Here's the code that puts
Alright I'm totally baffled. Here's my code: if ($password == $correct_password[0] && !isset($_COOKIE['user'])) {
I have written code to generate thumbnails from pdf files & save them as
Here is my code. <div> <object> <param name="movie" value="http://www.youtube.com/v/Cbspv1ZKR8o?version=3&amp;hl=en_US" /> <param name="allowFullScreen" value="true" />
http://www.roguevalleyroses.com/rose_list.php?search_id=&class=&height=&growth=&color=&bloom_size=&bloom_type=&shade=&fragrance=&disease=&rebloom=&thorns=&zone=&hybridizer=Ashdown%20Roses&date_range=&text=&view=&show=&page=4 This is the page. The code that queries the results is here: http://pastebin.com/d51bfa53f

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.