是否可以仅在一个类中包含代码? [英] Is it possible to include code only inside one class?

查看:185
本文介绍了是否可以仅在一个类中包含代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能自己解释一下。

I hope I can explain myself.

Supose我有下一个:

Supose I have next:

文件啊:

#include "C.h"

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

文件Bh :

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

是否可以在Ah中包含Ch?

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

我的问题是我包含的代码给了我很多与常规函数的冲突。它不是逐个纠正冲突的选择,因为它们有很多。此外,我的Ch代码只是一个测试代码:经过一些测试后,我将删除包含行。

My problem is that the code I've included is giving me a lot of conflicts with usual functions. It's not an option to correct conflicts one by one, because there is a huge set of them. Also, my "C.h" code included is only a test code: after some tests, I will delete the include line.

有没有办法'冒泡'我的包含?

Is there any way of 'bubbling' my include?

提前谢谢。

编辑:Ah和Bh位于同一名称空间。

A.h and B.h are on the same namespace.

推荐答案


是否可以在Ah中包含Ch?

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

不。据我所知。

如果您有名称冲突,只需在其他名称空间中包含Ch,如@ user202729提出。这可以提供帮助。

If you have name conflicts, just include C.h within an other namespace, as @user202729 proposed. This can help.

但我猜你在A中使用C进行测试而你不能在A中用C与C ++ Cli不兼容的实现或来自Bh的内容

But I guess you use C in A for tests and you cannot use it in C in A without the implementation which is not compatible to C++Cli or content from B.h.

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

We used the pimpl ideom (pointer to implementation). Example: c++/clr currently does not allow do be included directly and that's why sometimes you cannot use libraries you want to use (like C.h), because they do rely on the support of .

这是我的Ch(由所有其他标题使用)

This is my C.h ( used by all the other 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编译)

This is my C.cpp (compiled without /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();
            }

#include "C.h"

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

这篇关于是否可以仅在一个类中包含代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆