Split a C++ class declaration

后端 未结 5 845
野的像风
野的像风 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: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

提交回复
热议问题