2

UPDATE: Let me clarify my files and reiterate my question:

main.h

#include "other.h"

class MyClass
{
    public:
    void MyMethod();
    void AnotherMethod();

    OtherClass test;
}

main.cpp

#include "main.h"

void MyClass::MyMethod()
{
    OtherClass otherTemp;     // <--- instantitate OtherClass object
    otherTemp.OtherMethod();  // <--- in this method here, is where I want to also call MyClass::AnotherMethod()
}

void MyClass::AnotherMethod()
{
    // Some code
}

other.h

class OtherClass
{
    public:
    void OtherMethod();
}

other.cpp

#include "other.h"
void OtherClass::OtherMethod()
{
    // !!!! This is where I want to access MyClass::AnotherMethod(). How do I do it?
    // I can't just instantiate another MyClass object can I? Because if you look above in 
    // main.cpp, this method (OtherClass::OtherMethod()) was called from within
    // MyClass::MyMethod() already.
}

So basically what I want is this: you instantiate object A, which in turn instantiates object B, which in turn calls a method that is from object A. I'm sure something like this is possible, but it might just be poor design on my part. Any direction would be greatly appreciated.

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
  • Why do you need to access it in the `.h`? – kennytm Jul 31 '10 at 16:57
  • For another shot at the same kind of question, see http://stackoverflow.com/questions/1655096/c-class-its-base-class-and-circular-include-includes – pascal Jul 31 '10 at 17:10
  • I didn't mean for anyone to assume that I was doing my implementation's in the header file. I've reiterated and clarified my original question. – Jake Wilson Jul 31 '10 at 21:02
  • Ok, have you tried it? Depending on how you implement it you might end up with infinite recursion (`OtherMethod` calling `AnotherMethod` and ...) but that has nothing to do with cyclic dependencies --even if you should try to avoid cycles anyway. – David Rodríguez - dribeas Jul 31 '10 at 21:24
  • I've updated my answer to address the call to AnotherMethod – pascal Aug 01 '10 at 05:24

4 Answers4

3

Some do one .h/.cpp per class:

my.h

#ifndef MY_H
#define MY_H
#include "other.h"
class MyClass
{
    public:
    void MyMethod();

    OtherClass test;
}
#endif // MY_H

other.h

#ifndef OTHER_H
#define OTHER_H
class OtherClass
{
    public:
    void Othermethod();
}
#endif // OTHER_H

my.cpp

#include "my.h"
void MyClass::MyMethod() { }

other.cpp

#include "other.h"
#include "my.h"
void OtherClass::OtherMethod)()
{
   // (ab)using MyClass...
}

If you're using only Visual Studio, you could use #pragma once instead of #ifndef xx_h #define xx_h #endif // xx_h. EDIT: as the comment says, and also the related Wikipedia page, #pragma once is also supported (at least) by GCC.

UPDATE: About the updated question, unrelated to #include, but more about passing objects around...

MyClass already has an embedded instance of OtherClass, test. So, in MyMethod, it's probably more like:

void MyClass::MyMethod()
{
    test.OtherMethod();
}

And if OtherMethod needs to access the MyClass instance, pass this instance to OtherMethod either as a reference, or a pointer:

By reference

class OtherClass { public: void OtherMethod(MyClass &parent); }

void MyClass::MyMethod() { test.OtherMethod(*this); }

void OtherClass::OtherMethod(MyClass &parent)
{
   parent.AnotherMethod();
}

By Pointer

class OtherClass { public: void OtherMethod(MyClass *parent); }

void MyClass::MyMethod() { test.OtherMethod(this); }

void OtherClass::OtherMethod(MyClass *parent)
{
   if (parent == NULL) return;  // or any other kind of assert
   parent->AnotherMethod();
}
pascal
  • 3,287
  • 1
  • 17
  • 35
2

Define the function outside of either class in a C++ source file, not in the header:

void OtherClass :: Othermethod() {
   // call whatever you like here
}
2

Create a .cpp file:

#include "main.h"

void OtherClass::Othermethod()
{
    MyClass m; //ok :)
    m.MyMethod(); //also ok.
}

Implementations don't belong in headers anyway.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

Just #include it inside other.cpp

Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93