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.
Some do one .h/.cpp per class:
my.h
other.h
my.cpp
other.cpp
If you’re using only Visual Studio, you could use
#pragma onceinstead of#ifndef xx_h #define xx_h #endif // xx_h.EDIT: as the comment says, and also the related Wikipedia page,
#pragma onceis 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:And if OtherMethod needs to access the MyClass instance, pass this instance to OtherMethod either as a reference, or a pointer:
By reference
By Pointer