/** * Demonstrates *The Rule of Five `default`s* C++ idiom. * * @file the_rule_of_five_defaults.h * @author Florian Wolters * * @section License * * Copyright Florian Wolters 2015 (http://blog.florianwolters.de). * * Distributed under the Boost Software License, Version 1.0. (See accompanying * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ #ifndef FW_IDIOM_THE_RULE_OF_FIVE_DEFAULTS_H_ #define FW_IDIOM_THE_RULE_OF_FIVE_DEFAULTS_H_ #include #include namespace fw { namespace idiom { class TheRuleOfFiveDefaultsMixin { public: /** * Initializes a new instance of the TheRuleOfFiveDefaultsMixin class. */ TheRuleOfFiveDefaultsMixin() = default; /** * Finalizes an instance of the TheRuleOfFiveDefaultsMixin. * * Default destructor. */ virtual ~TheRuleOfFiveDefaultsMixin() = default; /** * Default copy constructor. */ TheRuleOfFiveDefaultsMixin(TheRuleOfFiveDefaultsMixin const&) = default; /** * Default copy assignment operator. */ TheRuleOfFiveDefaultsMixin& operator=( TheRuleOfFiveDefaultsMixin const&) = default; /** * Default move constructor. */ TheRuleOfFiveDefaultsMixin(TheRuleOfFiveDefaultsMixin&&) = default; /** * Default move assignment operator. */ TheRuleOfFiveDefaultsMixin& operator=(TheRuleOfFiveDefaultsMixin&&) = default; }; /** * The TheRuleOfFiveDefaults class demonstrates *The Rule of Five `default`s* * idiom. * * @author Florian Wolters */ class TheRuleOfFiveDefaults final : public TheRuleOfFiveDefaultsMixin { public: /** * Initializes a new instance of the TheRuleOfFiveDefaults class with the * specified string. * * @param kValue The string. */ explicit TheRuleOfFiveDefaults(std::string const& kValue) : value_{kValue} { // NOOP } private: /** * The value of this TheRuleOfFiveDefaults instance. */ std::string value_; }; } // namespace idiom } // namespace fw #endif // FW_IDIOM_THE_RULE_OF_FIVE_DEFAULTS_H_ /** * Runs the application. * * @return Always `0`. */ int main() { using fw::idiom::TheRuleOfFiveDefaults; // Complete constructor. TheRuleOfFiveDefaults the_rule_of_five_defaults{"hello, world"}; // Copy constructor. TheRuleOfFiveDefaults copy{the_rule_of_five_defaults}; // Move constructor. TheRuleOfFiveDefaults move{std::move(copy)}; // Copy assignment operator. copy = the_rule_of_five_defaults; // Move assignment operator (from rvalue). move = TheRuleOfFiveDefaults{"foo"}; // Move assignment operator (from lvalue). move = std::move(copy); // Destructor(s). }