自己实验C++

来源:互联网 发布:筑家易网络办公 编辑:程序博客网 时间:2024/05/23 20:49

VS2013开发环境下


#include <iostream>
#include <memory>
#include <map>
#include <vector>
#include <excpt.h>
using namespace std;
struct tagTest
{
int m_nA;
};


class CTest
{
public:
CTest(){ cout << "CTest()" << endl; }
};


void fun(int &value)
{
cout << "left reference" << endl;
}


void fun(int &&value)
{
cout << "right reference" << endl;
}


class OperaterTest
{
public:
OperaterTest() { cout << "OperaterTest()" << endl; }
OperaterTest& operator [] (int nPara)
{
cout << "OperaterTest& operator[]" << endl;
return *this;
}
void operator () (int nPara)
{
cout << "OperaterTest void operater()" << endl;
}
};




class ParaTest
{
private:
int m_a = 1;
public:
ParaTest () { cout << "ParaTest()" << endl; }
~ParaTest(){ cout << "~ParaTest()" << endl; }
ParaTest(int nPara) { cout << "ParaTest(int nPara)" << endl; }
ParaTest(float fPara) { cout << "ParaTest(float fPara)" << endl; }
void MyPrint(){ cout << "print MyPrint!" << endl; }
};
class OperaterTestEx
{
public:
OperaterTestEx() { cout << "OperaterTestEx()" << endl; }
OperaterTestEx& operator [] (ParaTest const & para)
{
cout << "OperaterTestEx& operator[]" << endl;
return *this;
}
void operator () (ParaTest  *para = NULL)
{
cout << "OperaterTestEx void operater()" << endl;
}
};


class Test
{
public:
Test(){ cout << "Test()" << endl; }
~Test(){ cout << "~Test()" << endl; }
};


class parent
{
public:
void f(int a){ cout << "parent::f()" << endl; }
int  g(int a){ cout << "parent::g()" << endl; return 1; }
virtual void h(int a){ cout << "parent::h()" << endl; }
};


class son :public parent
{
public:
void f(int a){ cout << "son::f()" << endl; }
void  g(int a){ cout << "son::g()" << endl;}
virtual void h(int a){ cout << "son::h()" << endl; }
};


int main()
{
cout << "------------1111111111-----------------" << endl;
//CTest test;
CTest();
cout << "------------2222222222-----------------" << endl;
//int &nTest = 2;   //ERROR
const int &nTest = 2;
int a = 2;
fun(a);                //left
fun(2);                //right
fun(std::move(a));     //right
cout << "------------3333333333-----------------" << endl;
OperaterTest()[1][2](1); 


cout << "------------4444444444-----------------" << endl;
const ParaTest & para = 1;
const ParaTest & para2 = 1.0f;


cout << "------------5555555555-----------------" << endl;
OperaterTestEx()[1][2][3]();


cout << "------------unique_ptr-----------------" << endl;
unique_ptr <ParaTest>(new ParaTest);
unique_ptr <ParaTest> u_ptr(new ParaTest);
u_ptr->MyPrint();
unique_ptr <ParaTest> uCopy;
//uCopy = u_ptr;    // error
uCopy = unique_ptr <ParaTest>(new ParaTest);


cout << "------------shared_ptr-----------------" << endl;
shared_ptr <ParaTest>(new ParaTest);
shared_ptr <ParaTest> s_ptr(new ParaTest);
s_ptr->MyPrint();
shared_ptr <ParaTest> sCopy;
sCopy = s_ptr;    // right
s_ptr->MyPrint();
sCopy->MyPrint();
sCopy = shared_ptr <ParaTest>(new ParaTest);
cout << "------------6666666666-----------------" << endl;
map<int, int> m_mapTest;
m_mapTest[1] = 10;
m_mapTest[2] = 20;
map<int, int> ::iterator it = m_mapTest.begin();
if (m_mapTest.find(2) != m_mapTest.end()) cout << "find it" << endl;
cout << "------------7777777777-----------------" << endl;
Test * pTest = new Test();
Test* pTest2 = std::move(new Test());
cout << "------------888888888-----------------" << endl;
Test ttt = *pTest;
cout << "------------99999999-----------------" << endl;
Test ttt2 = std::move(*pTest);
cout << "------------0000000000-----------------" << endl;
son Cson;
Cson.f(1);
Cson.g(1);
Cson.h(1);


parent * pParent = &Cson;
pParent->f(1);
pParent->g(1);
pParent->h(1);


parent Cparent;
Cparent.f(1);
Cparent.g(1);
Cparent.h(1);

son * pSon = dynamic_cast<son *>(&Cparent);
pSon->f(1);
pSon->g(1);
//pSon->h(1);  //error


parent *pParent2 = pSon;
pParent2->f(1);
pParent2->g(1);
//pParent2->h(1);  //error


int i = 1;
int j = (++i) + (--i) ;
cout << "------------111111111111-----------------" << endl;
const char * cbuffer = 1 ? "hehe" : "hehe1";


typedef unsigned int size_t; size_t u1ex = 3;
unsigned int u1 = 3;
//int nConst[u1]; //error
//int nConstEx[u1ex]; //ERROR
cout << "------------222222222222222-----------------" << endl;
std::vector<int> test2;
//cout << "vector =" << test2[0] << endl;  //error
if (test2.size()>0)  cout << "vector =" << test2[0] << endl;
test2.push_back(1);
if (test2.size()>0)  cout << "vector =" << test2[0] << endl;
for (auto value : test2)
{
cout << value <<endl;
}
cout << "------------33333333333-----------------" << endl;
map<int, int> m_map;
m_map.insert(make_pair<int, int>(1, 10));
m_map.insert(make_pair<int, int>(3, 20));
m_map.insert(make_pair<int, int>(4, 30));
m_map.insert(make_pair<int, int>(2, 40));
m_map.insert(make_pair<int, int>(5, 50));
for (auto value : m_map)
{
cout << "map key=" << value.first <<" value="<<value.second<< endl;
}
//sort(m_map.begin,m_map.end);
cout << "------------捕获空指针异常-----------------" << endl;
//---------------------------
try

//tagTest * ptagTest = nullptr;  //空指针异常不能被捕获(除非工程设置为捕获EHA异常)
tagTest * ptagTest = new tagTest; 
ptagTest->m_nA = 10;
}
catch (...)
{
cout << "exception..."<< endl;
}
//---------------------------


    //先将其它代码注释掉,再编译__try  __except 不然报错:Cannot use __try in functions that require object unwinding
//__try
//{  
// tagTest * ptagTest = nullptr;//空指针异常能被捕获
// //tagTest * ptagTest = new tagTest;
// ptagTest->m_nA = 10;
// //int *nPINT = nullptr;
// //*nPINT = 3;
//}
////GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH
//__except (EXCEPTION_EXECUTE_HANDLER)
//{
// cout << "exception..." << endl;
//}


cout << "--------------------end----------------" << endl;


//system("pause");
return 0;
}

0 0