How do I make main a friend of my class from within a library?

后端 未结 4 821
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 03:03

Please see my first attempt at answering this . I neglected to tell the whole story before in an attempt to simplify things. Turns out my example works! Sorry.

The w

4条回答
  •  心在旅途
    2021-01-12 03:08

    I don't think you actually want to do what you are doing. This really seems like a hack and a design problem. If you really want to expose the internals of your class in some specialized circumstance, you could make an accessor class which is also defined inside your library.

    Something like this might work (may need appropriate forward declarations, etc. -- this is just a starting point):

    class ProcessManagerAccessor
    {
    public:
        ProcessManagerAccessor(ProcessManager & pm) : pm_(pm) { }
    
        // add public methods to expose internals
        void test() { pm_.test(); }
    
    private:
        ProcessManager & pm_;
    };
    
    class ProcessManager
    {
    public:
        friend class ProcessManagerAccessor;
    
        // ...
    };
    
    // ...
    
    ProcessManager pm;
    ProcessManagerAccessor pma(pm);
    pma.test();
    

提交回复
热议问题