#include<iostream>
#include<string.h>
#include<cstring>
#include<vector>
#include<functional>
using pf_type = void(*)(int);
using func_type = std::function<void(int)>;
void myFun1(pf_type pf, int i)
{
pf(i);
}
void myFun2(func_type func, int i)
{
func(i);
}
class Spear {
friend std::ostream& operator<<(std::ostream& cout, const Spear& spear);
friend std::istream& operator>>(std::istream &cin,Spear& spear);
public:
int get_I() { return this->i; }
std::string get_name() { return this->name; };
Spear() = default;
Spear(int i_, std::string name_, int* pf_) : i(i_), name(name_), pf(pf_){}
Spear(const Spear& spear) : i(spear.i), name(spear.name), pf(new int(*spear.pf)) {}
virtual ~Spear() {
delete pf;
pf = nullptr;
std::cout << "~Spear()" << std::endl;
};
Spear operator+(const Spear& spear)
{
if (this == &spear)return *this;
this->i = spear.i;
return *this;
}
Spear& operator=(const Spear& spear)
{
this->name = spear.name;
this->i = spear.i;
return *this;
}
bool operator<(const Spear& spear)
{
return this->i < spear.i;
}
void operator()()
{
std::cout << " " << std::endl;
}
int operator[](unsigned i)
{
if (i<0 || i>this->vec.size()) return -1;
return vec[i];
}
Spear& operator++()
{
++i;
return *this;
}
Spear operator++(int)
{
Spear temp = *this;
++i;
return temp;
}
virtual void openSpear() const{ std::cout << "Spear : Open Spear" << std::endl; }
private:
int i;
std::string name;
int* pf;
std::vector<int> vec{ 1,2,3,4,5 };
};
std::ostream& operator<<(std::ostream& cout, const Spear& spear)
{
cout << spear.i;
return cout;
}
std::istream& operator>>(std::istream &cin, Spear& spear)
{
cin >> spear.i;
return cin;
}
class FireSpear : public Spear
{
public:
FireSpear(int i_ ,std::string name_,int *pf_,int i2_ ): Spear(i_,name_,pf_),i2(i2_){}
void openSpear()const override
{
std::cout << "FireSpear : Open Spear" << std::endl;
}
~FireSpear() {
std::cout << "~FireSpear()" << std::endl;
}
private:
int i2;
};
class Test {
public:
Test() = default;
Test(const Test& test)
{
if (test.str)
{
unsigned len = strlen(test.str) + 1;
str = new char[len]();
strcpy_s(str, len, test.str);
}
else {
str = nullptr;
}
}
Test(Test&& test)
{
if (test.str) {
str = test.str;
test.str = nullptr;
}
else
{
str = nullptr;
}
}
Test& operator= (const Test& test) {
if (this == &test)
{
return *this;
}
if (str)
{
delete[] str;
str = nullptr;
}
if (test.str)
{
unsigned len =strlen(test.str)+1;
str = new char[len]();
strcpy_s(str, len, test.str);
}
else {
str = nullptr;
}
return *this;
}
Test& operator= (Test&& test)
{
if (this == &test)
{
return *this;
}
if (str)
{
delete[] str;
str = nullptr;
}
if (test.str)
{
str = test.str;
test.str = nullptr;
}
else
{
str = nullptr;
}
return *this;
}
private:
char* str = nullptr;
};
Test makeTest()
{
Test t;
return t;
}
void test01()
{
Spear* pSpear = new FireSpear(20, "xzd", new int(20), 18);
pSpear->openSpear();
delete pSpear;
}
void test02()
{
int f = 100000;
myFun1([](int i) {
std::cout << i + 1 << std::endl;
}, 200);
myFun2([&](int i) {
std::cout << f * f << std::endl;
std::cout << i << std::endl;
}, 2000);
}
void test03()
{
Spear* pSpear = new FireSpear(20, "xzd", new int(20), 18);
if (std::string(typeid(*pSpear).name()) == "class FireSpear")
{
FireSpear* pFireSpear = dynamic_cast<FireSpear*>(pSpear);
if (pFireSpear)
{
std::cout << "transmit success" << std::endl;
}
else {
std::cout << "transmit failed" << std::endl;
}
}
else {
std::cout << "pSpear不指向FireSpear" << std::endl;
}
delete pSpear;
}
void test04()
{
Test t = makeTest();
}
int main()
{
test03();
return 0;
}