Back To Basics: The Rule of FiveC++ Core Guidelines ### Guideline C.21 is “The Rule of Five” ## Simple String ## Simple String - Constructor struct SString { String(char const * cp) : data_(new char[string(cp) + 1]) { string(data_ s{"Pass to a function by value"}; // ... somefn(s); // ... } ## Simple String - Copy Constructor struct SString { // ... String(SString const & rhs) : data_(new char[string(rhs to be moved!"}; // ... somefn(std::move(s)); // ... }; ## Simple String - Move Constructor struct SString { // ... SString(SString && rhs) noexcept : data_(rhs.data_) {0 码力 | 42 页 | 623.10 KB | 1 年前3
Back to Basics: Object-Oriented Programmingobject is created. Data fields of classes should be initialized in the constructor initialization list C. The body of the constructor is available for further operations. ■ Classes can consist of member Access specifier }; class Apple 4 : /*this->*/ mData{i} C Constructor initializer list 5 { D Constructor body 6 } 8 Member functions void Set(int i) { /*this->*/ mData = with override. Never override a non-virtual member function. ■ Prefer class when there is a constructor that checks the invariant, otherwise struct. ## I am Fertig https://AndreasFertig.com 












