Is it possible to include code only inside one class?

左心房为你撑大大i 提交于 2019-12-14 04:22:16

问题


I hope I can explain myself.

Supose I have next:

File "A.h":

#include "C.h"

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

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

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

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?

Thank you in advance.

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


回答1:


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

No. Not to my knowledge.


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


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.

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 .

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

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

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


来源:https://stackoverflow.com/questions/48583565/is-it-possible-to-include-code-only-inside-one-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!