C++包含和循环依赖

发布于 2024-09-11 22:42:48 字数 1243 浏览 4 评论 0原文

更新:让我澄清我的文件并重申我的问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你在我安 2024-09-18 22:42:48

有些为每个类执行一个 .h/.cpp:

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...
}

如果您仅使用 Visual Studio,则可以使用 #pragma Once 而不是 #ifndef xx_h #define xx_h #endif // xx_h
编辑:正如评论所说,还有相关的 维基百科页面#pragma Once(至少)也被 GCC 支持。

更新:
关于更新的问题,与 #include 无关,但更多关于传递对象...

MyClass 已经有一个 OtherClass 的嵌入实例,test。因此,在 MyMethod 中,它可能更像是:

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

如果 OtherMethod 需要访问 MyClass 实例,则将此实例作为引用或指针传递给 OtherMethod:

通过引用

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

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

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

通过指针

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();
}

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();
}
很酷又爱笑 2024-09-18 22:42:48

在 C++ 源文件中的任一类之外定义函数,而不是在标头中:

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

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

void OtherClass :: Othermethod() {
   // call whatever you like here
}
玩物 2024-09-18 22:42:48

创建 .cpp 文件:

#include "main.h"

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

无论如何,实现不属于标头。

Create a .cpp file:

#include "main.h"

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

Implementations don't belong in headers anyway.

爱的那么颓废 2024-09-18 22:42:48

只需将其 #include 到 other.cpp 中即可

Just #include it inside other.cpp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文