Best practice: ordering of public/protected/private within the class definition?

后端 未结 10 1317
遥遥无期
遥遥无期 2020-12-12 14:34

I am starting a new project from the ground up and want it to be clean / have good coding standards. In what order do the seasoned developers on here like to lay things out

10条回答
  •  伪装坚强ぢ
    2020-12-12 14:46

    The sequence of public followed by protected and private is more readable to me, It's better to describe the class logic in comments at top of the header file simply and function call orders to understand what a class dose and algorithms used inside.

    I am using Qt c++ for a while and see some new sort of keywords like signal and slot I prefer to keep ordering like above and share my idea with you here.

    #ifndef TEMPLATE_H
    #define TEMPLATE_H
    
    
    class ClassName
    {
        Q_OBJECT
        Q_PROPERTY(qreal startValue READ startValue WRITE setStartValue)
        Q_ENUMS(MyEnum)
    
    public:
    
        enum MyEnum {
            Hello = 0x0,
            World = 0x1
        };
    
        // constructors
    
        explicit ClassName(QObject *parent = Q_NULLPTR);
        ~ClassName();
    
        // getter and setters of member variables
    
        // public functions (normal & virtual) -> orderby logic
    
    public slots:
    
    signals:
    
    protected:
    
        // protected functions it's rule followed like public functions
    
    
    private slots:
    
    private:
    
        // methods
    
        // members
    
    };
    
    #endif // TEMPLATE_H
    

提交回复
热议问题