zoukankan      html  css  js  c++  java
  • c++学习2

    • C++多态
    • #include <iostream>
      #include <iomanip>
      using namespace std;
      
      class Base {
      public:
          virtual void f() { cout << "base" << endl; }
      };
      class Derived : public Base{
      public:
          void f() { cout << "derived" << endl; }
      };
      class Base2 {
      public:
          virtual void g() { cout << "base2 process g" << endl; } // dynamic_cast only work under polymorphic settings
      };
      class Derived2 : public Base2{
      public:
          void f() { cout << "derived2" << endl; }
      };
      
      int main() {
          /*
          Base *b = new Derived;
          b->f(); //base
      
          Base *b = new Derived;
          Derived *a = static_cast<Derived *>(b);
          a->f(); //derived
      
          Base *b = new Derived;
          b->f(); //derived, derived overwrite base in virtual table
          Derived *a = dynamic_cast<Derived *>(b);
          a->f(); //derived, same with b->f()
       */
          Base2 *b = new Derived2;
          //b->f(); // compile error
          Derived2 *a = dynamic_cast<Derived2 *>(b);
          a->f(); //derived2, same with b->f()
          return 0;
      }
    • 可调用函数, 匿名函数
    • auto f = [](int a, int b){return a + b;};
      cout << f(1,2) << endl; //3
      cout << [](int a, int b){return a + b;}(1,2) << endl; //3
      int a = 0; int b = 0; int c = 0;
      
      //cout << [=](int a, int b){c = 1; return a + b;}(1,2) << "	" << c << endl;
      // error: cannot assign to a variable captured by copy in a non-mutable lambda
      //cout << [](int a, int b){int c = 1; return a + b;}(1,2) << "	" << c << a << endl; //3 	 0
      
      cout << [&](int a, int b){c = 1; return a + b;}(1,2) << "	" << c << "	" << a << endl; //3 	 1
      cout << [&c](int a, int b){c = 1; return a + b;}(1,2) << "	" << c << endl; //3 	 1
      
    • C++ 宏下的 # 和 ##
      • #的功能是将其后面的宏参数进行字符串化操作
      • ##被称为连接符(concatenator)
    • /// Macro for registering a layer type.
      /// Example: REGISTER_LAYER(crf_error, CRFDecodingErrorLayer);
      #define REGISTER_LAYER(__type_name, __class_name) 
        static InitFunction __reg_type_##__type_name(   
            []() { Layer::registrar_.registerClass<__class_name>(#__type_name); })
      
    • C++explicit关键字详解
      •   http://www.cnblogs.com/ymy124/p/3632634.html
      • C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况下即声明为implicit(隐式). explicit关键字的作用就是防止类构造函数的隐式自动转换. explicit关键字只对有一个参数的类构造函数有效, 如果类构造函数参数大于或等于两个时, 是不会产生隐式转换的, 所以explicit关键字也就无效了。 但是 当除了第一个参数以外的其他参数都有默认值的时候, explicit关键字依然有效, 此时, 当调用构造函数时只传入一个参数, 等效于只有一个参数的类构造函数
  • 相关阅读:
    Vue源码探究-数据绑定的实现
    vue 数组遍历方法forEach和map的原理解析和实际应用
    vue 微信内H5调起支付
    uni-app官方教程学习手记
    vue-cli3 搭建的前端项目基础模板
    vue.js响应式原理解析与实现
    vue-waterfall2 基于Vue.js 瀑布流组件
    解决lucene更新删除无效的问题
    spring项目启动报错
    js监听页面copy事件添加版权信息
  • 原文地址:https://www.cnblogs.com/sylar120/p/6597578.html
Copyright © 2011-2022 走看看