• C++网易云课堂开发工程师--类模板


    static

    complex

    data members

    static data members

    member functions

    static member functions

    非静态成员函数:non-static member functions

    complex c1, c2, c3                                                                             complex c1, c2, c3

    cout << c1.real();                           ------------------>                         cout << complex::real(&c1);                      -----------------------> non-static data members

    cout << c2.real();                           ------------------>                         cout << complex::real(&c2);                      -----------------------> non-static data members

    static data members  静态数据  “仅有一份”

    static member functions   静态函数  -------------->  没有thispointer,所以静态函数只能处理静态数据

    进一步补充,static

    class Account{

      public:

        static double m_rate;

        static void set_rate(const double& x) {m_rate = x;} 静态函数没有thispointer所以只能调用静态数据

    };

    double Account::m_rate = 8.0;               静态数据必须在class外面进行定义(定义获取内存)

    int main(){

      Account::set_rate(5.0);                                                        调用static函数的方式有两种:

      Account a;                     (1) 通过object调用,通过对象调用

      a.set_rate(7.0);                    (2) 通过class name调用,通过类名调用

    }

     进一步补充,把ctors放在private中,把构造函数放在private中

    class A{

      public:

        static A& getInstance(return a;)                                      getInstance(return a;)外界可以通过a来获取对象

        setup()  {...}

      private:

        A();                                                                                      将A放在private里面,那么没有任何人可以创建A

        A(const A& rhs);

        static A a;

    }

    A::getInstance().setup();   单例设计模式;  通过调用函数来实现,调用自己

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    上述表例中,若没有自己则会造成浪费,例如static A a中, 若不需要创建则造成浪费。

    class A{

      public:

        static A& getInstance();

        setup() {...}

      private:

        A();

        A(const A& rhs);

        ...

    };

    A& A::getInstance(){                A::getInstance().setup();

      static A a;

      return a;

    }

    进一步补充:class template, 类模板

    template <typename T>

    class complex{

      public:

        complex(T r = 0, T i = 0)  :  re(r),   im(i)  {  }

        complex& operator += (const complex&);

        T   real()   const   {return re;}

        T   imag()     const   {return im;}

      private:

        T re, im;

        friend complex& _doapl(complex*, const complex&);

    };

    {

      complex<double>  c1(2.5, 1.5);

      complex<int>  c2(2, 6);

    }

    进一步补充:function template, 函数模板

    template <class T>

    inline

    const T& min(const T& a, const T& b){

      return b < a ? b : a;

    }

    类模板与函数模板的差别

    1.

    类模板在使用时,必须指定类型是什么?

    函数模板不必明确指出,编译器会进行参数推倒。

    进一步补充:namespace

    namespace std{           可以分为不同模块进行包装 

      ...

    }

    using directive      

    #include <iostream.h>

    using namespace std;         全部打开

    int main(){

      cin << ...;

      cout << ...;

      return 0;

    }

  • 相关阅读:
    被Play framework狠狠的play了一把
    ant导入Zookeeper到Eclipse错误path contains invalid character
    Hadoop2.4代码的坑
    uml类关系
    Hadoop 源码编译导出
    Eclipse 导入 Hadoop 源码
    js中的prototype和constructor
    react之路:使用redux-immutable
    react之路:使用actionCreators和constants
    react之路:使用combineReducers拆分reducer
  • 原文地址:https://www.cnblogs.com/sky-z/p/9568359.html
Copyright © 2020-2023  润新知