#include <iostream>
#include <type_traits>
#include <map>
#include <string>
#include <functional>
#include <memory>
struct A{
int a;
std::string name;
};
void fun(int)
{
std::cout << "fuck1" << std::endl;
}
void fun(void *)
{
std::cout << "fuck2" << std::endl;
}
void func(void)
{
std::cout << __FUNCTION__ << std::endl;
}
class Foo
{
public:
Foo(int _age, char *_p) :age(_age), point(_p)
{
}
static int foo_func(int a)
{
std::cout << __FUNCTION__ << "(" << a << ") ->: " << std::endl;
return a;
}
int foo_test(int i)
{
std::cout << __FUNCTION__ << "(" << i << ") ->: " << std::endl;
return i;
}
private:
int age;
char *point;
};
class Bar
{
public:
int operator() (int a)
{
std::cout << __FUNCTION__ << "(" << a << ") ->: ";
return a;
}
virtual ~Bar(){}
public:
int a;
int b;
};
class C : public Bar
{
private:
//char ca;
//sizeof 也包含Bar 的内容
public:
int a;
};
void testfunc(std::function<int(int)>& tf, int a)
{
tf(a);
}
int main() {
int type = 1000;
int var = 1234234;
std::string strvar = "hello";
char * buffer = new char[128];
Foo ge(232323, buffer);
Foo gee = ge;
// [lambda 表达式不能直接用外部变量 要将外部变量加入](参数形式){函数体}
// 外部变量传入方式
// [=] 传入所有外部变量 但是不能修改
// [&] 传入所有外部变量的引用 可以对其进行修改
// [=, &var] var 可以修改 其他都不能修改
// [var, &] var 不可以修改 其他都可以修改
[type](int a){std::cout << a << type << std::endl; }(12334);
auto lam = [&](int a, std::string b){
type = 123;
std::cout << a + type << std::endl;
std::cout << b << std::endl;
};
lam(456, "helloworld");
// 可以使用 -> std::string 指定返回值得类型
auto lam2 = [=, &strvar](std::string app) -> std::string {
std::cout << type << " " << var << std::endl;
strvar.append(app);
std::cout << strvar << std::endl;
return strvar;
};
auto tt = lam2("magegegegeghehehehe");
std::cout << "tt:------->" << tt << std::endl;
// 类似于函数指针 但是书写方便
std::function<void(void)> functemp = func;
void(*funpoint)() = func;
functemp();
funpoint();
std::function<int(int)> foofunc = Foo::foo_func;
foofunc(12);
Foo f(12, 0);
//延迟使用对象中的方法
std::function<int(int)> footest = std::bind(&Foo::foo_test, &f, std::placeholders::_1);
//footest();
testfunc(footest, 288888883);
//int(*footempfunc)(int) = &Foo::foo_func;
//footempfunc(123);
//NULL 和 nullptr 不同在于 NULL 其实就是宏变量0 nullptr 是指空指针
fun(nullptr);
std::map<int, int> imap;
imap[0] = 12;
imap[2] = 234;
struct A b = { 23, "testhello" };
// 让编译器自动推导参数类型
auto hh = b;
auto temp = imap;
auto x = 1;
auto y = 2;
decltype(x + y) z = 3;
//获取参数类型decltype
if (std::is_same<decltype(temp), std::map<int, int> >::value)
{
std::cout << "temp = std::map<int, int>" << std::endl;
}
if (std::is_same<decltype(hh), struct A>::value)
std::cout << "hh is struct A" << std::endl;
if (std::is_same<decltype(x), int>::value)
std::cout << "type x == int" << std::endl;
if (std::is_same<decltype(x), float>::value)
std::cout << "type z == float" << std::endl;
if (std::is_same<decltype(x), decltype(z)>::value)
std::cout << "type z == type x" << std::endl;
Bar bt2;
C aa;
Bar * btemp = new Bar();
int a = 1000;
// 可以是内置类型的转换 编译期间类型检查
char c = static_cast<char>(a);
C * ctemp = static_cast<C*>(btemp);
// 运行时期类型检查 为了提供RTTI﹐C++ 就将在VFT 中附加个指针﹐指向typeinfo对象﹐这对象内含RTTI资料.
//由于该类所实例化之各对象﹐皆含有个指针指向VFT 表﹐因之各对象皆可取出typeinfo对象而得到RTTI
C * ct2 = dynamic_cast<C*>(btemp);
std::cout << c << std::endl;
//dynamic_cast 不可以是内置类型转换 只能是类或者void * 类型的指针或者引用
std::cout <<"Bar size :"<<sizeof(Bar) << std::endl;
std::cout << "C size :" << sizeof(C) << std::endl;
return 0;
}