1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <cstring>
#include <complex>
#include <utility>
#include <algorithm>
using namespace std;
class Complex
{
public:
Complex(double, double)
{
cout << "Complex constructor\n";
}
Complex(Complex const &other)
{
cout << "Complex copy constructor\n";
}
~Complex()
{
cout << "Complex destructor\n";
}
Complex &operator=(Complex const &other)
{
cout << "Complex operator=\n";
}
};
class String
{
friend ostream &operator<<(ostream &out, String const &str);
string d_str;
public:
String(std::string const &txt)
:
d_str(txt)
{
cout << "String constructor\n";
}
String(String const &other)
:
d_str(other.d_str)
{
cout << "String copy constructor\n";
}
~String()
{
cout << "String destructor\n";
}
String &operator=(String const &other)
{
d_str = other.d_str;
cout << "String operator=\n";
}
};
ostream &operator<<(ostream &out, String const &str)
{
return out << str.d_str;
}
union Union
{
#if 0
typedef complex<double> Type1;
typedef string Type2;
#else
typedef Complex Type1;
typedef String Type2;
#endif
pair<int, int> u_int;
pair<int, Type1> u_complex;
pair<int, Type2> u_string;
Union(Union const &other);
Union(int i);
Union(double real, double imaginary);
Union(string const &str);
~Union();
int &asInt();
Type1 &asComplex();
Type2 &asString();
Union &operator=(Union &&tmp);
Union &operator=(Union const &rhs);
void swap(Union &other);
};
int &Union::asInt()
{
return u_int.second;
}
Union::Type1 &Union::asComplex()
{
return u_complex.second;
}
Union::Type2 &Union::asString()
{
return u_string.second;
}
Union::Union(Union const &other)
{
switch (other.u_int.first)
{
case 1:
new (&u_int) pair<int, int>(other.u_int);
break;
case 2:
new (&u_complex) pair<int, Type1>(other.u_complex);
break;
case 3:
new (&u_string) pair<int, Type2>(other.u_string);
break;
}
}
Union::Union(int i)
:
u_int(1, i)
{}
Union::Union(double real, double imaginary)
:
u_complex(2, {real, imaginary})
{}
Union::Union(string const &str)
:
u_string(3, str)
{}
Union::~Union()
{
switch (u_int.first)
{
case 2:
u_complex.second.~Type1();
break;
case 3:
u_string.second.~Type2();
break;
}
}
Union &Union::operator=(Union &&tmp)
{
swap(tmp);
return *this;
}
Union &Union::operator=(Union const &other)
{
Union tmp(other);
return *this = std::move(tmp);
}
void Union::swap(Union &other)
{
char memory[sizeof(Union)];
memcpy(memory, &other, sizeof(Union));
memcpy(&other, this, sizeof(Union));
memcpy(this, memory, sizeof(Union));
}
int main()
{
Union ustr("hello world");
Union ucom(12.4, 12.5);
ustr = ucom;
ustr = Union("hi there, again");
cout << ustr.asString() << '\n';
}
|