美文网首页
Boolan Week1笔记

Boolan Week1笔记

作者: Impluse | 来源:发表于2017-10-18 19:18 被阅读0次

    (1)头文件的防卫式声明:

    #ifndef __XXX__

    #define __XXX__

     …

    #endif

    (2)class template

    template<typename T>

    class XXX

    {

    //将类型换做T

    }

    {

    //用时具体指定T的类型

    XXX<int> …

    XXX<double>…

    }

    (3)inline

    函数若在class body内定义则自动成为inline候选人,否则可加inline,但是否会变成inline不确定。

    (4)access level 访问级别

    public 公开,private 私有,一般数据都需要private封存,有的函数也可以根据需要private。

    (5)initialization list  初值列  (构造函数的特有用法)

    XXX(T a=0, T b=0): A(a) , B(b) {}

    //XXX为类名,()内可以设置初值, A/B为封存的数据

    (6)const member functions 常量成员函数

    double real() const { return re; }

    // 不会改变数据内容的在 () 和 {} 之间加 const

    (7)pass by reference 传引用

    double //pass by value

    complex& //pass by reference

    // pass by reference 绝大多数时候优于 pass by value,尽量传reference,但如果函数内新产生的local object需要return,不可以传 reference。

    //传递者无需知道接受者是以reference形式接收。

    (8)friend 友元

    private:

    friend xxxx;

    //可以自由取得 friend 的 private 成员


    (!)相同class的各个objects互为friends

    (9)operator overloading 操作符重载

    inline complex&

    complex::operator += (const complex& r)

    {

            return __doapl (this, r);

    }//成员函数会自动把操作符左边的数据存为this不能写出来,但可以使用

    //全局函数无this

    * cout 的重载

    ostream& operator << (ostream& os, …)

    {

    return os << ‘ ’ << … ;

    }

    相关文章

      网友评论

          本文标题:Boolan Week1笔记

          本文链接:https://www.haomeiwen.com/subject/twmeuxtx.html