C++包含和循环依赖
更新:让我澄清我的文件并重申我的问题:
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.
}
所以基本上我想要的是这样:您实例化对象 A,对象 A 又实例化对象 B,对象 B 又调用对象 A 的方法。我确信这样的事情是可能的,但就我而言,这可能是糟糕的设计。任何方向将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有些为每个类执行一个 .h/.cpp:
my.h
other.h
my.cpp
other.cpp
如果您仅使用 Visual Studio,则可以使用
#pragma Once
而不是#ifndef xx_h #define xx_h #endif // xx_h
。编辑:正如评论所说,还有相关的 维基百科页面,
#pragma Once
(至少)也被 GCC 支持。更新:
关于更新的问题,与 #include 无关,但更多关于传递对象...
MyClass 已经有一个 OtherClass 的嵌入实例,
test
。因此,在 MyMethod 中,它可能更像是:如果 OtherMethod 需要访问 MyClass 实例,则将此实例作为引用或指针传递给 OtherMethod:
通过引用
通过指针
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 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: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
在 C++ 源文件中的任一类之外定义函数,而不是在标头中:
Define the function outside of either class in a C++ source file, not in the header:
创建 .cpp 文件:
无论如何,实现不属于标头。
Create a .cpp file:
Implementations don't belong in headers anyway.
只需将其 #include 到 other.cpp 中即可
Just #include it inside other.cpp