C++ Includes and Circular dependencies
UPDATE: Let me clarify my files and reiterate my question:
main.h
#include "other.h"
class MyClass
{
public:
开发者_Python百科 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.
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();
}
Define the function outside of either class in a C++ source file, not in the header:
void OtherClass :: Othermethod() {
// call whatever you like here
}
Create a .cpp file:
#include "main.h"
void OtherClass::Othermethod()
{
MyClass m; //ok :)
m.MyMethod(); //also ok.
}
Implementations don't belong in headers anyway.
Just #include it inside other.cpp
精彩评论