Last active
June 29, 2016 00:24
-
-
Save hgiesel/610231150fb6686a4e7a to your computer and use it in GitHub Desktop.
Examples of useful C++ snippets
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> | |
#include <string> | |
class Yen { | |
int _amount; | |
public: | |
Yen(size_t amount): _amount(amount) { /* */ } | |
friend auto operator<< (std::ostream& os, const Yen& yen) -> std::ostream& { | |
os << static_cast<int>(yen.amount()) << " Yen"; | |
return os; | |
} | |
virtual auto amount(void) const noexcept -> const int { | |
return this->_amount; | |
} | |
virtual void amount(const int amount) noexcept { | |
this->_amount = amount; | |
} | |
}; | |
auto operator"" _Y(unsigned long long yen) -> const Yen { | |
return Yen{yen}; | |
} | |
int main(void) { | |
Yen y{134}; | |
std::cout << y; | |
std::cout << 123_Y; | |
} |
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 <memory> | |
#include <iostream> | |
class Widget { | |
int _value; | |
std::unique_ptr<Widget> _successor = nullptr; | |
public: | |
Widget(void): _value(0) { /* */ }; | |
Widget(const Widget& rhs): _value(0) { /* */ }; | |
Widget(Widget&& rhs): _value(0) { /* */ }; | |
virtual ~Widget(void) { /* */ } | |
operator bool(void) const; | |
void value(int a); | |
auto value(void) const -> int; | |
void successor(Widget& successor); | |
void swap(Widget& swapper); | |
void reset(void); | |
void pass_value(int a); | |
auto pass_value(void) const -> int; | |
}; | |
Widget::operator bool(void) const { | |
return !!_successor; | |
} | |
void Widget::value(int a) { | |
this->_value = a; | |
} | |
int Widget::value(void) const { | |
return this->_value; | |
} | |
void Widget::successor(Widget* const& successor) { | |
this->_successor.reset(successor); | |
} | |
void Widget::swap(Widget& swapper) { | |
this->_successor.swap(swapper._successor); | |
} | |
void Widget::reset(void) { | |
this->_successor.reset(nullptr); | |
} | |
void Widget::pass_value(int a) { | |
_successor->_value = a; | |
} | |
int Widget::pass_value(void) const { | |
return _successor->value(); | |
} | |
void test(Widget& a, Widget& b, Widget& c) { | |
Widget* z{ new Widget{}}; | |
a.successor(z); | |
a.pass_value(123); | |
b.successor(new Widget{}); | |
b.pass_value(456); | |
// uncomment to see the world burn | |
// z will be pointed-to by two object, a and b, this will cause the std::unique_ptr in a to dangle later on | |
// b.reset(); | |
// b.pass_value(z); | |
c.successor(new Widget{}); | |
c.pass_value(789); | |
std::cout << "Step 1 ------------" << '\n'; | |
std::cout << "Widget a: " << a.pass_value() << std::endl; | |
std::cout << "Widget b: " << b.pass_value() << std::endl; | |
std::cout << "Widget c: " << c.pass_value() << std::endl; | |
std::cout << '\n'; | |
a.swap(b); | |
b.swap(c); | |
std::cout << "Step 2 ------------" << '\n'; | |
std::cout << "Widget a: " << a.pass_value() << std::endl; | |
std::cout << "Widget b: " << b.pass_value() << std::endl; | |
std::cout << "Widget c: " << c.pass_value() << std::endl; | |
std::cout << '\n'; | |
a.pass_value(1111111); | |
c.reset(); | |
} | |
int main(void) { | |
Widget a{}, b{}, c{}; | |
test(a, b, c); | |
std::cout << "Step 3 ------------" << '\n'; | |
if (a) std::cout << "Widget a: " << a.pass_value() << std::endl; | |
if (b) std::cout << "Widget b: " << b.pass_value() << std::endl; | |
if (c) std::cout << "Widget c: " << c.pass_value() << std::endl; | |
std::cout << '\n'; | |
} | |
// Expected Output: | |
// Step 1 ------------ | |
// Widget a: 123 | |
// Widget b: 456 | |
// Widget c: 789 | |
// | |
// Step 2 ------------ | |
// Widget a: 456 | |
// Widget b: 789 | |
// Widget c: 123 | |
// | |
// Step 3 ------------ | |
// Widget a: 1111111 | |
// Widget b: 789 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment