Created
January 31, 2015 15:38
-
-
Save FlorianWolters/26f0ea1117f34866f2a2 to your computer and use it in GitHub Desktop.
C++ source code examples for the weblog article "The Rule of Zero"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Demonstrates *The Rule of Five* C++ idiom. | |
* | |
* @file the_rule_of_five.h | |
* @author Florian Wolters <[email protected]> | |
* | |
* @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_H_ | |
#define FW_IDIOM_THE_RULE_OF_FIVE_H_ | |
#include <cstring> | |
#include <utility> | |
namespace fw { | |
namespace idiom { | |
/** | |
* The TheRuleOfFive class demonstrates *The Rule of Five* idiom. | |
* | |
* @author Florian Wolters <[email protected]> | |
*/ | |
class TheRuleOfFive final { | |
public: | |
/** | |
* Initializes a new instance of the TheRuleOfFive class with the specified | |
* C-style string. | |
* | |
* @param kValue The C-style string. | |
*/ | |
explicit TheRuleOfFive(char const* kValue) | |
: resource_{new char[std::strlen(kValue) + 1]} { | |
std::strcpy(resource_, kValue); | |
} | |
/** | |
* Finalizes an instance of the TheRuleOfFive class. | |
* | |
* This is the destructor. | |
*/ | |
~TheRuleOfFive() { | |
delete[] resource_; | |
} | |
/** | |
* Initializes a new instance of the TheRuleOfFive class from the specified | |
* TheRuleOfFive. | |
* | |
* This is the copy constructor. | |
* | |
* @param kValue The TheRuleOfFive to copy. | |
*/ | |
TheRuleOfFive(TheRuleOfFive const& kOther) | |
: resource_{new char[std::strlen(kOther.resource_) + 1]} { | |
std::strcpy(resource_, kOther.resource_); | |
} | |
/** | |
* Assigns the specified TheRuleOfFive to this TheRuleOfFive. | |
* | |
* This is the copy assignment operator. | |
* | |
* @param kValue The TheRuleOfFive to assign to this TheRuleOfFive. | |
*/ | |
TheRuleOfFive& operator=(TheRuleOfFive& kOther) { | |
std::swap(resource_, kOther.resource_); | |
return *this; | |
} | |
/** | |
* Initializes a new instance of the TheRuleOfFive class from the specified | |
* TheRuleOfFive. | |
* | |
* This is the move constructor. | |
* | |
* @param kValue The TheRuleOfFive to copy. | |
*/ | |
TheRuleOfFive(TheRuleOfFive&& other) : resource_{other.resource_} { | |
other.resource_ = nullptr; | |
} | |
/** | |
* Assigns the specified TheRuleOfFive to this TheRuleOfFive. | |
* | |
* This is the move assignment operator. | |
* | |
* @param kValue The TheRuleOfFive to assign to this TheRuleOfFive. | |
*/ | |
TheRuleOfFive& operator=(TheRuleOfFive&& other) { | |
std::swap(resource_, other.resource_); | |
return *this; | |
} | |
private: | |
/** | |
* The resource (a raw pointer to a character) to handle by this class. | |
*/ | |
char* resource_; | |
}; | |
} // namespace idiom | |
} // namespace fw | |
#endif // FW_IDIOM_THE_RULE_OF_FIVE_H_ | |
/** | |
* Runs the application. | |
* | |
* @return Always `0`. | |
*/ | |
int main() { | |
using fw::idiom::TheRuleOfFive; | |
// Complete constructor. | |
TheRuleOfFive the_rule_of_five_defaults{"hello, world"}; | |
// Copy constructor. | |
TheRuleOfFive copy{the_rule_of_five_defaults}; | |
// Move constructor. | |
TheRuleOfFive move{std::move(copy)}; | |
// Copy assignment operator. | |
copy = the_rule_of_five_defaults; | |
// Move assignment operator (from rvalue). | |
move = TheRuleOfFive{"foo"}; | |
// Move assignment operator (from lvalue). | |
move = std::move(copy); | |
// Destructor(s). | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Demonstrates *The Rule of Five `default`s* C++ idiom. | |
* | |
* @file the_rule_of_five_defaults.h | |
* @author Florian Wolters <[email protected]> | |
* | |
* @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 <string> | |
#include <utility> | |
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 <[email protected]> | |
*/ | |
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). | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Demonstrates *The Rule of Three* C++ idiom. | |
* | |
* @file the_rule_of_three.h | |
* @author Florian Wolters <[email protected]> | |
* | |
* @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_THREE_H_ | |
#define FW_IDIOM_THE_RULE_OF_THREE_H_ | |
#include <cstring> | |
#include <utility> | |
namespace fw { | |
namespace idiom { | |
/** | |
* The TheRuleOfThree class demonstrates *The Rule of Three* idiom. | |
* | |
* @author Florian Wolters <[email protected]> | |
*/ | |
class TheRuleOfThree final { | |
public: | |
/** | |
* Initializes a new instance of the TheRuleOfThree class with the specified | |
* C-style string. | |
* | |
* @param kValue The C-style string. | |
*/ | |
explicit TheRuleOfThree(char const* kValue) | |
: resource_{new char[std::strlen(kValue) + 1]} { | |
std::strcpy(resource_, kValue); | |
} | |
/** | |
* Finalizes an instance of the TheRuleOfThree class. | |
* | |
* This is the destructor. | |
*/ | |
~TheRuleOfThree() { | |
delete[] resource_; | |
} | |
/** | |
* Initializes a new instance of the TheRuleOfThree class from the specified | |
* TheRuleOfThree. | |
* | |
* This is the copy constructor. | |
* | |
* @param kValue The TheRuleOfThree to copy. | |
*/ | |
TheRuleOfThree(TheRuleOfThree const& kOther) | |
: resource_{new char[std::strlen(kOther.resource_) + 1]} { | |
std::strcpy(resource_, kOther.resource_); | |
} | |
/** | |
* Assigns the specified TheRuleOfThree to this TheRuleOfThree. | |
* | |
* This is the copy assignment operator. | |
* | |
* @param kValue The TheRuleOfThree to assign to this | |
* TheRuleOfThree. | |
*/ | |
TheRuleOfThree& operator=(TheRuleOfThree& kOther) { | |
std::swap(resource_, kOther.resource_); | |
return *this; | |
} | |
private: | |
/** | |
* The resource (a raw pointer to a character) to handle by this class. | |
*/ | |
char* resource_; | |
}; | |
} // namespace idiom | |
} // namespace fw | |
#endif // FW_IDIOM_THE_RULE_OF_THREE_H_ | |
int main() { | |
using fw::idiom::TheRuleOfThree; | |
// Complete constructor. | |
TheRuleOfThree the_rule_of_three{"hello, world"}; | |
// Copy constructor. | |
TheRuleOfThree copy{the_rule_of_three}; | |
// Copy assignment operator. | |
copy = the_rule_of_three; | |
// Destructor(s). | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Demonstrates *The Rule of Zero* C++ idiom. | |
* | |
* @file the_rule_of_zero.h | |
* @author Florian Wolters <[email protected]> | |
* | |
* @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_ZERO_H_ | |
#define FW_IDIOM_THE_RULE_OF_ZERO_H_ | |
#include <string> | |
#include <utility> | |
namespace fw { | |
namespace idiom { | |
/** | |
* The TheRuleOfZero class demonstrates *The Rule of Zero* idiom. | |
* | |
* @author Florian Wolters <[email protected]> | |
*/ | |
class TheRuleOfZero final { | |
public: | |
/** | |
* Initializes a new instance of the TheRuleOfZero class with the specified | |
* string. | |
* | |
* @param kValue Thestring. | |
*/ | |
explicit TheRuleOfZero(std::string const& kValue) : value_{kValue} { | |
// NOOP | |
} | |
private: | |
/** | |
* The value of this TheRuleOfZero instance. | |
*/ | |
std::string value_; | |
}; | |
} // namespace idiom | |
} // namespace fw | |
#endif // FW_IDIOM_THE_RULE_OF_ZERO_H_ | |
/** | |
* Runs the application. | |
* | |
* @return Always `0`. | |
*/ | |
int main() { | |
using fw::idiom::TheRuleOfZero; | |
// Complete constructor. | |
TheRuleOfZero the_rule_of_five_defaults{"hello, world"}; | |
// Copy constructor. | |
TheRuleOfZero copy{the_rule_of_five_defaults}; | |
// Move constructor. | |
TheRuleOfZero move{std::move(copy)}; | |
// Copy assignment operator. | |
copy = the_rule_of_five_defaults; | |
// Move assignment operator (from rvalue). | |
move = TheRuleOfZero{"foo"}; | |
// Move assignment operator (from lvalue). | |
move = std::move(copy); | |
// Destructor(s). | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment