4.2 普通拷贝

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
#include <iostream>

class Address
{
public:
std::string street, city;
int suite;
};

class Contact
{
public:
std::string name;
Address address;
};

int main(void)
{
Contact worker{"", Address{"123 East Dr", "London", 0}};
Contact john = worker;

john.name = "John Doe";
john.address.suite = 10;

return 0;
}

4.3 通过拷贝构造函数进行拷贝

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
#include <iostream>

class Address
{
public:
std::string street, city;
int suite;
Address(const std::string& street, const std::string& city, const int suite)
: street{street}, city{city}, suite{suite} {}
};

class Contact
{
public:
std::string name;
Address* address;
Contact(const std::string name, Address* address)
: name{name}
, address{address} {}
Contact(const Contact& other)
: name{other.name}
, address{ new Address{*other.address} } {}

Contact& operator=(const Contact& other)
{
if (this == &other)
return *this;
name = other.name;
address = other.address;
return *this;
}
};

int main(void)
{
Contact worker{"", new Address{"123 East Dr", "London", 0}};
Contact john{worker};

john.name = "John Doe";
(*john.address).suite = 10;
return 0;
}

4.4 “虚”构造函数

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
#include <iostream>

class Address
{
public:
std::string street, city;
int suite;
Address(const std::string& street, const std::string& city, const int suite)
: street{street}, city{city}, suite{suite} {}

virtual Address* clone()
{
return new Address{street, city, suite};
}
};

class Contact
{
public:
std::string name;
Address* address;
Contact(const std::string name, Address* address)
: name{name}
, address{address} {}
Contact(const Contact& other)
: name{other.name}
, address{ new Address{*other.address} } {}


Contact& operator=(const Contact& other)
{
if (this == &other)
return *this;
name = other.name;
address = other.address;
return *this;
}
};

class ExtendedAddress : public Address
{
public:
std::string country, postcode;
ExtendedAddress(const std::string &street, const std::string &city,
const int suite, const std::string &country,
const std::string &postcode)
: Address(street, city, suite)
, country{country}, postcode{postcode} {}

ExtendedAddress* clone() override
{
return new ExtendedAddress(street, city, suite,
country, postcode);
}
};

int main(void)
{
ExtendedAddress ea{"123 West Dr", "London", 123, "UK", "SW101EG"};
Address& a = ea;
auto cloned = a.clone();

return 0;
}

4.5 序列化

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
#include <iostream>
#include <algorithm>
#include <string>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class Address
{
public:
std::string street;
std::string city;
int suite;
private:

friend class boost::serialization::access;
template<class Ar> void serialize(
Ar &ar,
const unsigned int version)
{
ar & street;
ar & city;
ar & suite;
}
};

class Contact
{
public:
std::string name;
Address* address = nullptr;
private:
friend class boost::serialization::access;
template<class Ar> void serialize(Ar& ar,
const unsigned int version)
{
ar & name;
ar & address;
}
};

template <typename T> T clone(T obj)
{
//1. Serialize the object
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << obj;
std::string s = oss.str();

//2. Deserialize it
std::istringstream iss(oss.str());
boost::archive::text_iarchive ia(iss);
T result;
ia >> result;

return result;
}

int main(void)
{
Contact john{"xx", new Address{"..", "..", 2}};

Contact jane = clone(john);
jane.name = "Jane";
return 0;
}

4.6 原型工厂

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
#include <iostream>
#include <memory>

class Address;
class Contact;
class EmployeeFactory;

class Address
{
public:
std::string street, city;
int suite;
Address(const std::string& street, const std::string& city, const int suite)
: street{street}, city{city}, suite{suite} {}

virtual Address* clone()
{
return new Address{street, city, suite};
}
};

class Contact
{
public:
friend class EmployeeFactory;
std::string name;
Address* address;
Contact(const std::string name, Address* address)
: name{name}
, address{address} {}
Contact(const Contact& other)
: name{other.name}
, address{ new Address{*other.address} } {}


Contact& operator=(const Contact& other)
{
if (this == &other)
return *this;
name = other.name;
address = other.address;
return *this;
}
};

class EmployeeFactory
{
static Contact mai;
static Contact aux;

static std::unique_ptr<Contact> NewEmployee(
std::string name, int suite, Contact& proto)
{
auto result = std::make_unique<Contact>(proto);
result->name = name;
result->address->suite = suite;
return result;
}
public:
static std::unique_ptr<Contact> NewMainOfficeEmployee(
std::string name, int suite)
{
return NewEmployee(name, suite, mai);
}

static std::unique_ptr<Contact> MaxAuxOfficeEmployee(
std::string name, int suite)
{
return NewEmployee(name, suite, aux);
}
};

Contact EmployeeFactory::mai{"", new Address{"123 East Dr", "London", 0}};
Contact EmployeeFactory::aux{"", new Address{"123 East Dr", "London", 0}};

int main(void)
{
auto john = EmployeeFactory::NewMainOfficeEmployee("John Doe", 123);
return 0;
}