Split a C++ class declaration

后端 未结 5 799
野的像风
野的像风 2021-01-06 01:46

I want to know if I can split a C++ class declaration

Original class

    class P
    {
        private: 
           int id;
           //some really          


        
相关标签:
5条回答
  • 2021-01-06 02:00

    Something like this?

    class P
    {
    private:
        class Impl
        {
        public:
           int id;
           //some really secret method
           int secretMethod();
        };
    
    private:
        Impl* _pimpl;
    
    protected:
        int x;
    
    public:
        P() : _pimpl(new P::Impl()) {}
        ~P() { delete _pimpl; } 
        int getX();
    
    };
    
    0 讨论(0)
  • 2021-01-06 02:06

    It is not possible to genuinely split a C++ class definition. All that you can do is implement a run-time abstraction which will firewall the source code by using a disgusting hack like PIMPL.

    0 讨论(0)
  • 2021-01-06 02:08

    If your purpose is to simply reduce clutter in your header, you can include a file in the middle of your class:

    class P
    {
    #include "P.private_parts"
    
       protected:
           int x;
       public:
           P();
           int getX();
    };
    
    0 讨论(0)
  • 2021-01-06 02:10

    You can heritage the second part like this:

    //Class P_Hetitage definition

    class P_Heritage {
          protected:
                    int id;
                    //some really secret method
                    int secretMethod();
    }
    

    //Class P definition

    class P : private P_Heritage {
          protected:
                    int x;
          public:
                 P();
                 int getX();
    };
    

    Below a straightforward explanation how inheritance works:

    Inheritance the class P_Heritage as:

    public

    1. public elements are public to class P
    2. protected elements are private to class P

    private

    1. public elements are private to class P
    2. protected elements are private to class P

    P_Heritage's private elements can not be seen by class P

    0 讨论(0)
  • 2021-01-06 02:13

    Yes, it is possible but not in the direct kind of way. Here is what you do:

    my_object.h:

    struct my_object {
      void fun();
    
      my_object();
      my_object(my_object const&);
      ~my_object();
    
      my_object& operator = (my_object);
    
    protected:
      void pfun();
    
    private:
      struct impl;
      std::unique_ptr<impl> pimpl;
    };
    

    my_object.cpp:

    struct my_object::impl {
      void fun() { do stuff...}
    
      void pfun() { do other stuff... }
    
      int private_member;
    };
    
    my_object::my_object() : pimpl(new impl) {}
    my_object::my_object(my_object const& o) : pimpl(new impl(*o.pimpl) {}
    my_object::~my_object() {}
    
    my_object& my_object::operator = (my_object o) { swap(pimpl, o.pimpl); return *this; }
    
    void my_object::fun() { pimpl->fun(); }
    void my_object::pfun() { pimpl->pfun(); }
    

    As you can see, it's a lot of work and requires the heap. Everything in balance...use when you need to.

    0 讨论(0)
提交回复
热议问题