2

I have two classes, lets say Class A and Class B. My goal is to have both classes use eachothers functions. Problem is, the multi-file include structure doesn't seem to let me do that. Here's what I'm trying to do:

#file A.h

Class A{
public:
    int GetInfo();

private:
    B * ptrToB;
};

#file B.h

Class B{
public:
   int getStuff();
private:
   A * ptrToA;
};

My goal is for an A class method to be able to call ptrToB->getStuff() and for a B class method to be able to call ptrToA->getInfo()

Is this possible? How so? If not, why not?

ollo
  • 24,797
  • 14
  • 106
  • 155
Nathan
  • 73,987
  • 14
  • 40
  • 69
  • 1
    Others have pointed the way to make it compile, but ask yourself why your classes need to be so coupled. Either make it one class, or put the right data on the right class. Don't use getters. Tell the class with the data to do the right thing with that data. Look up [Data Envy](http://c2.com/cgi/wiki?DataEnvy), and others. – Peter Wood May 15 '13 at 19:48
  • possible duplicate of [Mutually recursive classes](http://stackoverflow.com/questions/3410637/mutually-recursive-classes) – ecatmur May 16 '13 at 08:09

3 Answers3

4

Maybe using forward declarations?

#file A.h

#ifndef ACLASS_H
#define ACLASS_H

Class B;

Class A{
public:
    int GetInfo();

private:
    B * ptrToB;
};

#endif

Then in the CPP file.

#file A.cpp

#include "B.h"

A::A() : ptrToB(0)
{
  // Somehow get B
}

int A::GetInfo() 
{
  // Return whatever you need in here.
}

The same thing would be done for the B classes H and CPP files.

A forward definition allows the compiler to recognize a type without needed it explicitly defined. If you had a reference to a B in the A class you would have to include B's header.

Since you are using a pointer to access B the compiler does not need to know the internal data until you access it (inside the CPP file).

// Would need the header because we must know 
// the size of B at compile time.
class B;
class A 
{
  B theB; 
}


// Only need forward declaration because a 
// pointers size is always known by the compiler
class B;
class A
{
  B * bPointer; 
}
Connor Hollis
  • 1,115
  • 1
  • 7
  • 13
  • Thanks, it works! I tried this earlier, except I tried calling aPointer->AMemberFunc() from an inline function in the B.h file. I guess the key to this is forward declarations in the header file, followed by the include in the .cpp file? Is this specifically to avoid circular-dependencies? – Nathan May 15 '13 at 22:12
  • Yeah you can do that to either hide the implementation of something and to avoid circular dependencies. – Connor Hollis May 16 '13 at 01:40
  • I have only one .cpp file and Is it possible to write above both classes in a single file?. – Vijay Kumbhani Aug 13 '15 at 12:48
  • I'm not sure what you mean? Do you want to forward declare both classes? If so that is valid. Otherwise give me an example or make a new question. – Connor Hollis Aug 13 '15 at 16:05
2

Just add a forward declaration to file A.h, so the compiler knows that a B* is a pointer to a class you will define later on:

class B;

Then define your class A and include B.h after that. This way anybody including A.h will have both class A and class B defined.

In B.h just include A.h at the beginning. This way anybody including B.h will also have both class A and class B defined.

When you define your functions in the associated .cpp files, you will have both classes available, and can just write your functions as you want to.

This problem is called mutual recursion.

Community
  • 1
  • 1
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
  • +1 for explanation rather than just the code ready to be used... (which is of course also okay ;)) Good answer. – leemes May 15 '13 at 19:48
2

You can use forward declaration to break dependencies :

#file A.h

Class A{
public:
    int GetInfo();

private:
    B * ptrToB;
};

#file B.h
struct A;
Class B{
public:
   int getStuff();
private:
   A * ptrToA;
};

Then you can include A.h in B.cpp and B.h in A.cpp without problems.

BЈовић
  • 62,405
  • 41
  • 173
  • 273