是否可以只在一个 class 中包含代码?

Is it possible to include code only inside one class?

我希望我能解释一下。

假设我有下一个:

文件"A.h":

#include "C.h"

public class A{
    // Some code...
}

文件"B.h":

#include "A.h"

public class B{
    A a = new A();      //With this line I mean I'm using one instance of "A" inside "B.h"
    //Some code...
}

是否可以仅在 "A.h" 中包含 "C.h"?

我的问题是我包含的代码与常用功能有很多冲突。这不是一个一个地纠正冲突的选择,因为有大量的冲突。另外,我包含的"C.h"代码只是一个测试代码:经过一些测试,我将删除包含行。

有什么方法可以'bubbling'我的include吗?

提前致谢。

编辑:A.h 和 B.h 在同一个命名空间中。

Is it possible to include "C.h" ONLY inside "A.h"?

没有。据我所知。


如果您有名称冲突,只需将 C.h 包含在其他命名空间中,如 @user202729 所建议的。这可以提供帮助。


但我猜你在 A 中使用 C 进行测试,如果没有与 C++Cli 不兼容的实现或来自 B.h.

的内容,你就不能在 A 中的 C 中使用它

我们使用了 pimpl 概念(指向实现的指针)。 例子: c++/clr 目前不允许直接包含 do,这就是为什么有时你不能使用你想使用的库(比如 C.h),因为它们确实依赖于 .

这是我的C.h(其他人都用headers)

            struct LockImpl; // forward declaration of C.

            class C
            {
            public:
                C();
                virtual ~C();

            public:
                void Lock() const;
                void Unlock() const;
                LockImpl* _Lock;
            };

这是我的 C.cpp(没有 /clr 编译)

            #include <mutex>

            struct LockImpl
            {
                std::mutex mutex;
            };

            C::C() : _Lock(new LockImpl()) {}
            C::~C() { delete _Lock; }

            void C::Lock() const
            {
                _Lock->mutex.lock();
            }

            void C::Unlock() const
            {
                _Lock->mutex.unlock();
            }

A.h

#include "C.h"

public class A{
   C c;
   void someMethod()
   {
      c.Lock() // I used another template for a RAII pattern class.
      c.Unlock() 
   }
}