zoukankan      html  css  js  c++  java
  • <<Exceptional C++>> notes

     -

    class Complex
    {
    public:
        explicit Complex(double real, double imaginary = 0)
        : real_(real), imaginary_(imaginary)
        {
        }
    
        Complex& operaor+=(const Complex& o)
        {
             real_ += o._real_;
             imaginary_ += o.imaginary_;
             return *this;
        }
    
        Complex& operator++()
        {
             ++real_;
             return *this;
        }
    
        const Complex operator++(int)// to avoid this expression: a++++
        {
         Complex tmp(*this);
             ++*this;
             return tmp;
        }
    
        ostream& Print(ostream& os) const
        {
            return os << ....;
        }
    private:
        double real_, imaginary_;
    };
    
    const Complex operator+(const Complex& a, const Complex& b)
    {
        Complex ret(a);
        ret += b;
        return ret;
    }
    
    ostream& operator<<(ostream& os, const Complex& c)
    {
        return c.Print(os);
    }
    class Complex

    - My simple implementation for class string:

    https://github.com/yaoyansi/mymagicbox/blob/master/mystring/src/mystring.h

    https://github.com/yaoyansi/mymagicbox/blob/master/mystring/src/mystring.cpp

     

    - 避免使用公有 虚函数, 应该使用Template Method模式

    - #include<iosfwd>

    - operator new(), operator delete()都是static函数, 即使他们没有被声明为static

    - 永远不要多态地使用数组. 使用vector<>/deque<>而不要使用数组

    - 一般来说, 避免编写自动转换(隐式类型转换)的代码, 即转换操作符

    - 不要编写非显示构造函数

    - T t//调用默认构造函数

    T t()//声明了一个函数

    T t(u)//直接初始化, 用u初始化变量t

    T t=u// 调用拷贝构造函数

    尽可能使用T t(u), 而非T t=u

    - 当非内置返回类型使用by value返回时, 最好返回一个 const 值

    - 正确使用mutable, 是正确使用const的关键

    -

    class A { public: virtual ~A(); }

    class B : private virtual A {};

    class C : public A {}

    class D : public B, public C {}

    A a1; B b1; C c1; D d1;

    const A a2;

    const A& ra1 = a1;

    const A& ra2 = a2;

    char c;

    A *pa;  B *pb; C *pc;

    pa = (A*)& ra1;// pa = const_cast<A*>(&ra1)

    pa = (A*)&a2;//因为a2是一个const对象, 所以结果未定义

    pb = (B*)&c1;//pb = reinterpret_cast<B*>(&c1)

    pc = (C*)&d1;//C里是错误的, c++里不需要转换

    - 不要转调const, 使用mutable代替.

    - 不要使用inline, 除非性能分析显示它是必要的

    - 不要使用全局或静态变量. 如果要用, 需要注意他们的初始化顺序.

    - 在构造函数初始化列表中的基类, 应该按照其在类声明中出现的顺序列出.

    - 在构造函数初始化列表中的成员变量, 应该按照其在类声明中出现的顺序列出.

  • 相关阅读:
    python学习-装饰器
    python-内置函数
    HA高可用解决方案-RHCS部署
    mac安装nose,command not found:nosetests
    iPhone的home键进果汁了,按起来粘粘的感觉
    Reportng 的测试报告在 Jenkins 中显示不全
    atom markdown报错:AssertionError: html-pdf: Failed to load PhantomJS module.
    markdown的图片外链
    reportng定制修改
    运行maven build报错No goals have been specified for this build.
  • 原文地址:https://www.cnblogs.com/yaoyansi/p/4012354.html
Copyright © 2011-2022 走看看