-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
59 lines (46 loc) · 1.58 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <list>
#include <memory>
class CTest {
public:
CTest( std::string testName ) {
std::cout << "Constructor - CTest" << std::endl;
this->testName = testName;
}
~CTest() {
std::cout << std::endl;
std::cout << "Destructor - CTest" << std::endl;
//Print the name of the object when it gets
//deleted
std::cout << "Name: " << testName << std::endl;
}
void addTest( std::shared_ptr<CTest> test ) {
testList.push_back( test );
}
private:
std::string testName;
//List of shared pointers (CTest)
std::list<std::shared_ptr<CTest>> testList;
};
int main(int argc, const char **argv ) {
CTest root( "Root" );
//Explicit objects that also contain childs
std::shared_ptr<CTest> leftSide( new CTest( "LeftSide" ) );
std::shared_ptr<CTest> rightSide( new CTest( "RightSide" ) );
//Add left and right side of the tree
root.addTest( leftSide );
root.addTest( rightSide );
//Add objects to the left side
//ATTENTION:
// If smart pointers are used this way (no local variable that contains the
// the smart pointer object) the creation of the raw pointer can and should
// only depend on the constructor!!!
leftSide->addTest( std::shared_ptr<CTest>( new CTest( "Object one" ) ) );
leftSide->addTest( std::shared_ptr<CTest>( new CTest( "Object two" ) ) );
//Add objects to the right side
rightSide->addTest( std::shared_ptr<CTest>( new CTest( "Object three" ) ) );
rightSide->addTest( std::shared_ptr<CTest>( new CTest( "Object four" ) ) );
rightSide->addTest( std::shared_ptr<CTest>( new CTest( "Object five" ) ) );
return 0;
}