Last active
November 8, 2017 16:45
-
-
Save jpclipffel/f9a38f16747bdbcf1f99 to your computer and use it in GitHub Desktop.
C++ 11 class prototype
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
#include "class.hh" | |
/** | |
* Default constructor. | |
* Initialize class attributs using initialization list. | |
*/ | |
myClass::myClass(): | |
number(42), | |
letter('A') | |
{ | |
} | |
/** | |
* Copy constructor. | |
* Initialize attributs (using initialization list) from _other to this. | |
* @param _other Object to copy from. | |
*/ | |
myClass::myClass(const myClass& _other): | |
number(_other.number), | |
letter(_other.letter) | |
{ | |
} | |
/** | |
* Move constructor. | |
* Calling default constructor to initialize attributs - works only in C++11. | |
* @param _other Object to swap with. | |
*/ | |
myClass::myClass(myClass&& _other) noexcept: | |
myClass() | |
{ | |
swap(*this, _other); | |
} | |
/** | |
* Destructor. | |
*/ | |
myClass::~myClass() | |
{ | |
} | |
/** | |
* Assignement operator. | |
* @param _other Object to copy. | |
*/ | |
myClass& myClass::operator=(myClass _other) | |
{ | |
swap(*this, _other); | |
return *this; | |
} | |
/** | |
* Move assignement operator. | |
* @param _other Object to copy. | |
*/ | |
myClass& myClass::operator=(myClass&& _other) noexcept | |
{ | |
swap(*this, _other); | |
return *this; | |
} | |
/** | |
* std::string cast method. | |
*/ | |
myClass::operator std::string() | |
{ | |
return "myClass"; | |
} | |
/** | |
* Functor example. | |
*/ | |
void myClass::operator()() | |
{ | |
} | |
/** | |
* Class swaping method. | |
* Use std::swap to swap attributs. | |
* @param _self First object (this) to swap. | |
* @param _other Second object (foreign) to swap. | |
*/ | |
void myClass::swap(myClass& _self, myClass& _other) noexcept | |
{ | |
std::swap(_self.number, _other.number); | |
std::swap(_self.letter, _other.letter); | |
} | |
/** | |
* Reset object initial state. Useful if objet should be re-used later. | |
*/ | |
void myClass::reset() | |
{ | |
number = 42; | |
letter = 'A'; | |
} | |
/** | |
* Exception constructor. | |
* @param _msg Error message. | |
*/ | |
myClass::error::error(const std::string &_msg): | |
std::runtime_error(_msg) | |
{ | |
} | |
/** | |
* Comparison. | |
* Effectively compare two objects. | |
* @param _one Comparison item 1. | |
* @param _two Comparison item 2. | |
*/ | |
bool operator==(const myClass& _one, const myClass& _two) | |
{ | |
if ( | |
_one.number == _two.number && | |
_one.letter == _two.letter | |
) | |
return true; | |
return false; | |
} | |
/** | |
* Reverse-comparison operator. | |
* Return inverse of comparison. | |
* @param _one Comparison item 1. | |
* @param _two Comparison item 2. | |
*/ | |
bool operator!=(const myClass& _one, const myClass& _two) | |
{ | |
return !(_one == _two); | |
} | |
bool operator>(const myClass&_one, const myClass& _two) | |
{ | |
return (_one.number > _two.number); | |
} | |
bool operator>=(const myClass&_one, const myClass& _two) | |
{ | |
return (_one.number >= _two.number); | |
} | |
bool operator<(const myClass&_one, const myClass& _two) | |
{ | |
return (_one.number < _two.number); | |
} | |
bool operator<=(const myClass&_one, const myClass& _two) | |
{ | |
return (_one.number <= _two.number); | |
} | |
/** | |
* Output stream operator. | |
* Write object content to an std::ostream. | |
* @param os Output stream. | |
* @param _self Object to output. | |
*/ | |
std::ostream& operator<<(std::ostream& os, const myClass& _self) | |
{ | |
// Write output: os << ... << ... | |
return os; | |
} | |
/** | |
* Input stream operator. | |
* Read content to object using a std::istream. | |
* @param is Input stream. | |
* @param _self Object to write in. | |
*/ | |
std::istream& operator>>(std::istream& is, myClass& _self) | |
{ | |
// Read input: is >> ... >> ... | |
return is; | |
} | |
/** | |
* Input string operator. | |
* Insert string content into object. | |
* @param is Input string. | |
* @param _self Object to write in. | |
*/ | |
void operator>>(const std::string& is, myClass& _self) | |
{ | |
// Read / parse / ... the string | |
} |
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
#include <iostream> // Serialization (optionnal) | |
#include <string> // Serialization (optionnal) | |
#include <stdexcept> // Exception (optionnal) | |
/** | |
* Example C++11 class. | |
* This class is compliant with most common C++ rules. | |
*/ | |
class myClass | |
{ | |
public: | |
// Mandatory constructors and destructor | |
myClass(); /**< Default constructor. */ | |
myClass(const myClass&); /**< Copy constructor. */ | |
myClass(myClass&&) noexcept; /**< Move constructor. */ | |
virtual ~myClass() noexcept; /**< Destructor. */ | |
void swap(myClass&, myClass&) noexcept; /**< Swap wrapper. */ | |
// Mandatory assignement operators | |
myClass& operator=(const myClass); /**< Copy assignement operator.*/ | |
myClass& operator=(myClass&&) noexcept; /**< Move assignement operator. */ | |
// Mandatory comparison operators | |
friend bool operator==(const myClass&, const myClass&); /**< Comparison operator. */ | |
friend bool operator!=(const myClass&, const myClass&); /**< Reverse comparison operator. */ | |
// Recomended comparison operators | |
friend bool operator<(const myClass&, const myClass&); /**< '<' operator. */ | |
friend bool operator<=(const myClass&, const myClass&); /**< '<=' operator. */ | |
friend bool operator>(const myClass&, const myClass&); /**< '>' operator. */ | |
friend bool operator>=(const myClass&, const myClass&); /**< '>=' operator. */ | |
// Optionnal serialization | |
operator std::string(); /**< Cast to string. */ | |
myClass& operator=(const std::string&); /**< Assign from string. */ | |
friend std::ostream& operator<<(std::ostream&, const myClass&); /**< Write content to stream. */ | |
friend std::istream& operator>>(std::istream&, myClass&); /**< Get content from stream. */ | |
friend void operator>>(const std::string&, myClass&); /**< Get content from string. */ | |
// Optionnal components | |
void operator()(); /**< Functor call. */ | |
void reset(); /**< Reset object initial state. */ | |
// Optionnal exception | |
class error: public std::runtime_error | |
{ | |
public: | |
error(const std::string& = ""); | |
}; | |
private: | |
int number; | |
char letter; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment