#include <iostream>
#include <vector>
void Test1()
{
double a = 10.5;
int b = 20;
int c = (int)a;
double d = (double)b;
int sc = static_cast<int>(a);
}
class Base
{
virtual void Test(){};
};
class Derived : public Base
{
void Test()
{
std::cout << "overwrite" << std::endl;
}
};
void Update(Derived *pd)
{
std::cout << "pd ptr:" << pd << std::endl;
}
void UpdateRefer(Derived &pd)
{
std::cout << "pd ptr:" << &pd << std::endl;
}
void Test2()
{
Derived d;
const Derived &crd = d;
Update(const_cast<Derived *>(&crd));
Update((Derived *)&crd);
Base *pb = new Derived();
}
void Test3()
{
Base *pb = new Base();
Update(dynamic_cast<Derived *>(pb));
try
{
UpdateRefer(dynamic_cast<Derived &>(*pb));
}
catch (std::exception e)
{
std::cout << e.what() << std::endl;
}
int a =10;
double b = 20.5;
Derived rd;
const Derived& crd = rd;
}
using funcType = void(*)();
int print()
{
return 0;
}
void Test4()
{
funcType funcArray[10];
funcArray[0] = reinterpret_cast<funcType>(print);
}
int main()
{
Test3();
return 0;
}
|