Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 127 lines (94 sloc) 2.655 kb
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
#include <iostream>
#include <memory>

///////////////////////////////////////////////////////////////////////////////
class Test
{
public:
  Test():m_value(0) { std::cout << "Test::Test" << std::endl; }
Test(const Test &t):m_value(0) { std::cout << "Test::Test copy" << std::endl; }
~Test() { std::cout << "Test::~Test destructor" << std::endl; }

int m_value;
};

typedef std::auto_ptr<Test> TestAutoPtr;
typedef std::unique_ptr<Test> TestUniquePtr;

///////////////////////////////////////////////////////////////////////////////
void AutoPtrTest();
void UniquePtrTest();
void UniquePtrArrayTest();
void SharedPtrTest();
void SharedPtrArrayTest();
void SharedPtrParamTest();
///////////////////////////////////////////////////////////////////////////////

void main()
{
//AutoPtrTest();

//UniquePtrTest();

//UniquePtrArrayTest();

//SharedPtrTest();

//SharedPtrArrayTest();

SharedPtrParamTest();

int a = 0;
return;
}

///////////////////////////////////////////////////////////////////////////////
// auto ptr tests

void doSomethig(TestAutoPtr myPtr)
{
myPtr->m_value = 11;
}

void AutoPtrTest()
{
TestAutoPtr myTest(new Test());
doSomethig(myTest);

myTest->m_value = 10;
}

///////////////////////////////////////////////////////////////////////////////
// unique ptr tests

void doSomethig(TestUniquePtr myPtr)
{
myPtr->m_value = 11;
}

void UniquePtrTest()
{
TestUniquePtr myTest(new Test());
doSomethig(std::move(myTest));

// myTest->m_value = 10; // runtime error, but this time on purpose!
}

///////////////////////////////////////////////////////////////////////////////
// unique ptr array tests

void UniquePtrArrayTest()
{
// posiible, but generates runtime error
// std::unique_ptr<int> p(new int[10]); // only delete will be called

std::unique_ptr<Test[]> tests(new Test[3]);
}

///////////////////////////////////////////////////////////////////////////////
// shared ptr tests

void SharedPtrTest()
{
std::shared_ptr<Test> sp(new Test());

std::shared_ptr<Test> sp2 = std::make_shared<Test>();

int a;
}

///////////////////////////////////////////////////////////////////////////////
// shared ptr array tests

void SharedPtrArrayTest()
{
std::shared_ptr<Test> sp(new Test[2], [](Test *p) { delete [] p; });
}

///////////////////////////////////////////////////////////////////////////////
// shared ptr param tests

void testSharedFunc(std::shared_ptr<Test> sp)
{
sp->m_value = 10;
}

void testSharedFuncRef(const std::shared_ptr<Test> &sp)
{
sp->m_value = 10;
}

void SharedPtrParamTest()
{
std::shared_ptr<Test> sp = std::make_shared<Test>();

testSharedFunc(sp);
testSharedFuncRef(sp);
}
Something went wrong with that request. Please try again.