54 lines
1.3 KiB
C++
54 lines
1.3 KiB
C++
#include "SmartPtr.h"
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
int main()
|
|
{
|
|
SmartPtr<int> s;
|
|
SmartPtr<int> x = SmartPtr<int>(new int(5));
|
|
SmartPtr<int> y(x);
|
|
s = x;
|
|
std::cout<<*x.get()<<std::endl;
|
|
std::cout<<*y.get()<<std::endl;
|
|
std::cout<<*x<<std::endl;
|
|
std::cout<<*y<<std::endl;
|
|
y.reset(NULL);
|
|
|
|
SmartPtr<int> R = SmartPtr<int>(new int(15));
|
|
std::cout<<*R.get()<<std::endl;
|
|
delete R.release();
|
|
SmartPtr<int> T = SmartPtr<int>(new int(25));
|
|
SmartPtr<int> U(T);
|
|
std::cout<<*U.get()<<std::endl;
|
|
std::vector<std::vector<std::string> > vec(1,std::vector<std::string>(10));
|
|
|
|
//vector
|
|
std::vector<SmartPtr<int> > myvec;
|
|
for(long i = 0; i < 10; i++)
|
|
{
|
|
myvec.push_back(SmartPtr<int>(new int(i)));
|
|
std::cout<<*myvec[i]<<"\t";
|
|
}
|
|
std::cout<<std::endl;
|
|
|
|
const std::vector<int*> myvec_raw = SmartPtr<int>::GetRawPtrContainer(myvec);
|
|
for(long i = 0; i < myvec_raw.size(); i++)
|
|
{
|
|
const int* const ptr = myvec_raw[i];
|
|
std::cout<<*ptr<<"\t";
|
|
}
|
|
std::cout<<std::endl;
|
|
|
|
int* ptr = new int(2);
|
|
int& i = *ptr;
|
|
std::cout<<i<<std::endl; //prints 2
|
|
ptr = new int(3);
|
|
std::cout<<*ptr<<std::endl; //prints 3
|
|
std::cout<<i<<std::endl; //still prints 2!
|
|
|
|
|
|
std::cout<<"End program"<<std::endl;
|
|
return 0;
|
|
}
|