Smart Pointers

I messing with smart pointers.
I would like to define a function that takes a copy of a smart pointer and does not mess with the original.

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

class Test
{
public:
	Test(std::string _value);
	Test(const Test& test); 
	Test& operator = (const Test& that);
	~Test(){};
	void setValue(std::string _value) { value = _value; };
	void setInt(int _value){ i = _value; };
private:
	std::string value;
	int i = 10;
};

Test::Test(std::string _value)
{
	value = _value;
}

Test::Test(const Test& test)
{
	value = test.value; 
	i = test.i;
}

Test& Test::operator=(const Test& that)
{
	value = that.value;
	i = that.i;
	return *this;
};

void doit(const Test& value);

void doita(const std::unique_ptr<Test>& value);

int _tmain(int argc, _TCHAR* argv[])
{
	std::shared_ptr<Test> aa(new Test("TEST"));
	aa->setInt(20);
	doit(*aa.get());
	//doita(aa)
	return 0;
}

void doit(const Test& value)
{
	//i want a copy of value that does not change original
	std::unique_ptr<Test> b(new Test(value));
	b->setInt(10);
}

void doita(const std::unique_ptr<Test>& value)
{
	//i want a copy of value that does not change original
	value.get()->setInt(10);
}
Topic archived. No new replies allowed.