美文网首页初见
cpp复习笔记3

cpp复习笔记3

作者: 芝麻酱的简书 | 来源:发表于2019-12-27 12:11 被阅读0次

    静态成员

    静态成员也是存储在全局数据区,跟全局变量的区别是 作用域。
    静态成员必须在类的外面初始化,且不能带static关键字:

    class Person {
    protected:
        static int age;
    };
    
    int Person::age = 10;
    

    可以通过类名访问:Person::age = 30;
    如果类的声明和实现分离,那么在实现中初始化静态成员变量。

    静态成员函数

    内部不能使用this指针,只能访问静态成员函数和静态成员变量。

    class Person {
    public:
        static int age;
        
        static void setAge(int newAge) {
            age = newAge;
        }
    };
    
    int Person::age = 10;
    

    单例模式:
    1.构造函数私有化
    2.定义一个私有的静态成员变量指针,用于指向单例对象
    3.提供一个公共的静态成员函数,用于返回单例对象

    class Person {
    public:
        static Person *shared() {
            //  这个地方还需要加锁
            if (person == nullptr) {
                person = new Person();
            }
            return person;
        }
        
    private:
        static Person *person;
        Person(){
            
        }
    };
    


    十一

    const成员变量

    必须在类内部初始化。可以声明的时候直接赋初值。
    staticconst成员变量还可以在初始化列表中,进行初始化。

    class Person {
        
        const int height = 180;
        
        void run() const {
            
        }
    };
    
    const成员函数

    const关键字写在参数列表后面,函数的声明和实现都必须加const
    函数内部不能修改 非static 的成员变量
    内部只能调用const成员函数、static成员函数
    const成员函数可以调用const成员函数
    const成员函数 对 非const成员函数构成重载
    const对象(指针)优先调用非const成员函数
    const对象(指针)只能调用const成员函数

    拷贝构造函数
    class Person {
        
        int age;
        int height;
        
        Person(int a, int h): age(a),height(h) { }
    
        // 拷贝构造函数 固定格式
        Person(const Person &person): age(person.age), height(person.height) {}
    };
    

    带字符串的构造函数 深拷贝:

    class Person {
        
        int age;
        char *name;
        
    public:
        
        // const是为了兼容外部传入const字符串
        Person(int age = 0, const char *name = nullptr): age(age) {
    
        }
        
        // 拷贝构造函数
        Person(const Person &p): age(p.age) {
            if (p.name == nullptr) { return; }
            
            // 申请堆空间来存储字符串内容, 否则存储的内容是指向栈空间的 {}是重置数据
            this->name = new char [strlen(name) + 1]{};
            
            // 拷贝字符串到堆空间
            strcpy(this->name, name);
            
        }
        
        ~Person() {
            if (this->name == nullptr) { return; }
            delete [] this->name; // 对应创建时候的[]
            this->name = nullptr;
        }
    };
     
    int main(int argc, const char * argv[]) {
        
        Person *p1 = new Person(10, "123123");
        char name[] = {'z', 'm', 'j'};
        Person *p2 = new Person(10, name);
        Person *p3 = p2;
        
        return 0;
    }
    
    
    对象类型的参数 和 返回值
    struct Person {
    
    };
    
    void test(Person p) {
        // 是一个浅拷贝的过程  会产生临时对象A
    }
    
    void test1(Person &p) {
        // 不会产生临时对象 传递的就是传入那个对象
    }
    
    void test2(const Person &p) {
        // 不产生临时对象 同时限制内部不能更改外部的对象 只能访问
    }
    


    十二

    匿名对象
    struct Person { };
    
    int main(int argc, const char * argv[]) {
        
        Person();
        return 0;
    }
    
    
    隐式构造
    struct Person {
        int age;
        Person(int age): age(age) { }
    };
    
    int main(int argc, const char * argv[]) {
        
        Person p(10);
        p = 20; // 隐式构造 相当于 p = Person(20);
        
        return 0;
    }
    
    编译器自动生成构造函数

    编译器在以下四种情况下,会自动给类生成无参的构造函数:

    1. 成员变量在声明的同时进行了初始化
    2. 有定义虚函数
    3. 虚继承自其他类
    4. 包含了对象类型的成员,且这个成员有构造函数(编译器生成或者用户自定义)
    友元

    1.友元包括友元函数友元类
    2.如果将函数A(非成员函数)声明为类C的友元函数,那么函数A就能直接访问类C对象的所有成员
    3.如果将类A声明为类C的友元类,那么类A的所有成员函数都能直接访问类C对象的所有成员
    4.友元破坏了面向对象的封装性,但是在某些频繁访问成员变量的地方,可以提高性能

    class Point {
        int x;
        int y;
        
        friend Point addPoint(const Point &p1, const Point &p2) {
            return Point(p1.x + p2.x, p1.y + p2.y);
        }
    public:
        Point(int x, int y): x(x),y(y) {}
    };
    
    内部类

    类的内部定义类

    局部类

    函数内部定义类

    运算符重载
    class Point {
        int x;
        int y;
        friend Point operator+(const Point &, const Point &);
    public:
        Point(int x, int y): x(x),y(y) {}
        // 使用引用作为返回值类型 来返回自身 避免产生临时变量
        Point &operator+=(const Point &point) {
            this->x += point.x;
            this->y += point.y;
            return *this;
        }
    };
    
    Point operator+(const Point &p1, const Point &p2) {
        return Point(p1.x + p2.x, p1.y + p2.y);
    }
    
    int main(int argc, const char * argv[]) {
        
        Point p1(1, 2);
        Point p2(2, 3);
    
        Point p3 = p1 + p2;
    
        (p1 += p2) = Point(10, 12);
        
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:cpp复习笔记3

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