Skip to content

Classes and Structs

Hannes Hauswedell edited this page Feb 22, 2017 · 19 revisions

General Design

POD/Aggregate preferred (shall be struct, otherwise shall be class and always specify rule of 5/6 constructors (ok to = default or = deleted, but always make explicit).

struct My_aggregate_type
{
  int my_member{7}; // use direct member initializers if necessary
};

class My_non_aggregate_type
{
// always specify public, protected, private in this order
public: 
    // Rule-of-six is usually desired
    My_non_aggregate_type() = default;
    // Rule-of-five    
    constexpr My_non_aggregate_type(My_non_aggregate_type const &) = default;
    constexpr My_non_aggregate_type(My_non_aggregate_type &&) = default;
    constexpr My_non_aggregate_type & operator =(My_non_aggregate_type const &) = default;
    constexpr My_non_aggregate_type & operator =(My_non_aggregate_type &&) = default;

protected:
    // make destructor protected to prevent dynamic polymorphism (unless explicitly desired)
    constexpr ~My_non_aggregate_type() = default;
private:
    int my_member{7}; // use direct member initializers if necessary
};

Static polymorphism (via concepts)

Dynamic polymorphism

Clone this wiki locally