Skip to content

Instantly share code, notes, and snippets.

@OTL
Last active July 2, 2018 11:08
Show Gist options
  • Save OTL/78b71cdae9d1894cc24303b46ba0d8c3 to your computer and use it in GitHub Desktop.
Save OTL/78b71cdae9d1894cc24303b46ba0d8c3 to your computer and use it in GitHub Desktop.
c++は難しい
#include <iostream>
class Widget {
public:
explicit Widget(int a, int b) : m_a(a), m_b(b) {
std::cout << "construct" << std::endl;
}
void echo() const {
std::cout << m_a << std::endl;
}
Widget(const Widget &w) : m_a(w.m_a), m_b(w.m_b) {
std::cout << "copy construct" << std::endl;
}
Widget(Widget &&w) : m_a(w.m_a), m_b(w.m_b) {
std::cout << "move construct" << std::endl;
}
Widget &operator=(const Widget &w) = default;
Widget &operator=(Widget &&w) = default;
private:
const int m_a;
int m_b;
};
Widget func_a(int a) {
const Widget w(a, 1);
if (a > 0) {
return w;
}
Widget w2(a, -1);
return w2;
}
Widget func_b(int a) {
const Widget w(a, 1);
return w;
}
int main(int argc, char **argv) {
#ifndef TEST2
const Widget w1 = func_a(1);
w1.echo();
const Widget w2 = func_a(-1);
w2.echo();
const Widget w3 = func_b(2)
w3.echo();
#else
Widget w4(1, 2);
w4 = func_b(2);
w4.echo();
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment