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
class NonCopyable { | |
public: | |
NonCopyable(const NonCopyable&) = delete; | |
NonCopyable& operator=(const NonCopyable&) = delete; | |
private: | |
... | |
} |
this way is much better than the old way where the programmer had to declare private both member and then not implement them getting an error either at compile time or either at linking time.
The delete specifier can disable some automatic overloads consider indeed the following code
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
class DoublePrinter { | |
public: | |
void print(double aDouble) { | |
std::cout << "Value is: " << aDouble << std::endl; | |
} | |
}; | |
DoublePrinter dp; | |
dp.print(3.3f); |
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
class DoublePrinter { | |
public: | |
void print(double aDouble) { | |
std::cout << "Value is: " << aDouble << std::endl; | |
} | |
void print(float) = delete; | |
}; |
The following class is a perfectly working class but used wrongly can store a reference to a temporary object
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
class CrashDestination { | |
public: | |
CrashDestination(const std::string& aString) | |
: theString(aString) | |
{} | |
void print() const { std::cout << theString << std::endl; } | |
private: | |
const std::string& theString; | |
}; | |
CrashDestination cd("TEMPORARY_STRING"); | |
cd.print(); |
The delete specifier can help us again indeed we can disable the constructor with a rvalue reference and avoid such use:
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
class CrashDestination { | |
public: | |
CrashDestination(const std::string& aString) | |
: theString(aString) | |
{} | |
CrashDestination(const std::string&&) = delete; | |
void print() const { std::cout << theString << std::endl; } | |
private: | |
const std::string& theString; | |
}; |
now the code above will lead to a compilation error in case we are trying to build the class with a temporary string, you should note that a "const &&" is needed, indeed without the const specifier passing a "const std::string foo()" will not led to a compilation error
Time to add to my coding rules a new rule!