C/C++(2)

2020-06-21  本文已影响0人  BlueFishMan

对象和类

过程性编程/面向对象编程(Object-oriented programming,OOP)

抽象/封装/数据隐藏

访问控制

访问控制

静态数据成员

静态成员函数

class Base {
    int a;
    static int b;
    static int func() {
        a;//no
        b;//yes
        object.a;//yes
        return 0;
    }
};
int Base::b;
Base::func();

Base c;
c.b;
c.func();

对象的存储空间

派生/继承

基类(父类)—>派生类(子类)


派生类的构造函数和析构函数

多态(公有继承)

具有多种形态,即同一个方法的行为随上下文而异

静态绑定/动态绑定

将源代码中的函数调用解释为执行特定的函数代码块被称为函数名绑定(binding)

静态绑定(static binding)/早绑定(early binding)

编译期

动态绑定(dynamic binding)/晚绑定(late binding)

运行期

class Shape {
public:
    //静态绑定
    int perimeter() {
        return 0;
    }

    //虚函数,动态绑定
    virtual int area() {
        return 0;
    }

    //纯虚函数
    virtual int vertex() = 0;
};

class Triangle : public Shape {
public:
    int perimeter() {//hide
        return 3;
    }

    int area() {//override
        return 2;
    }

    int vertex() {
        return 3;
    };
};

class Rectangle : public Shape {
public:
    int perimeter() {//hide
        return 4;
    }

    int area() {//override
        return 4;
    }

    int vertex() {
        return 4;
    };
};

int main() {
    Shape *shape;
    Triangle tri;
    Rectangle rec;

    shape = &tri;
    cout << shape->perimeter() << endl;//0,静态绑定
    cout << shape->area() << endl;//2,动态绑定

    shape = &rec;
    cout << shape->perimeter() << endl;//0
    cout << shape->area() << endl;//4

    return 0;
}

编译器处理虚函数的方法

编译器处理虚函数的方法

有关虚函数注意事项

智能指针

内存泄漏(memory leak)->析构函数->悬挂指针


explicit(显示)构造函数,指针作为参数

explicit

auto_ptr(deprecated in C++11)

#include <memory>
auto_ptr<int> a(new int(1));
auto_ptr<int> b;
b = a;// a loses ownership

unique_ptr

unique_ptr<int> a;
a = unique<int>(new int(1));//临时右值

unique_ptr<int> b;
b = std::move(a)//return a new object

unique_ptr<int[]> c(new int[3]{1, 2, 3});

shared_ptr

单例模式

class Singleton {
public:
    static Singleton *instance();

private:
    static Singleton *pInstance;
};

Singleton *Singleton::pInstance = nullptr;

Singleton *Singleton::instance() {
    if (pInstance == nullptr) {
        pInstance = new Singleton;
    }
    return pInstance;
}
Singleton *Singleton::instance() {
    pthread_mutex_lock(&mutex_x);
    if (pInstance == nullptr) {
        pInstance = new Singleton;
    }
    pthread_mutex_unlock(&mutex_x);
    return pInstance;
}
Singleton *Singleton::instance() {
    if (pInstance == nullptr) {
        pthread_mutex_lock(&mutex_x);
        if (pInstance == nullptr) {
            pInstance = new Singleton;
        }
        pthread_mutex_unlock(&mutex_x);
    }

    return pInstance;
}
pInstance = new Singleton;
Step 1: Allocate memory to hold a Singleton object.
Step 2: Construct a Singleton object in the allocated memory.
Step 3: Make pInstance point to the allocated memory.
Singleton *Singleton::instance() {
    Singleton *tmp = pInstance;
    // insert memory barrier
    if (tmp == nullptr) {
        pthread_mutex_lock(&mutex_x);
        if (tmp == nullptr) {
            tmp = new Singleton;
            // insert memory barrier
            pInstance = tmp;
        }
        pthread_mutex_unlock(&mutex_x);
    }

    return tmp;
}
上一篇 下一篇

猜你喜欢

热点阅读