C++ 友元,内部类,运算符重载

2020-11-10  本文已影响0人  lieon

友元

class Point {
    // add 是point的友元函数,可以Point类中的所有成员)
    friend Point add(const Point &, const Point &);
    // Math是Point的友元类,Math中可以访问Point类中的所有成员
    friend class Math;
private:
    int m_x;
    int m_y;
    
public:
    Point() {}
    Point(int x, int y): m_x(x), m_y(y) {}
};

Point add(const Point &p1, const Point &p2) {
    return Point(p1.m_x + p2.m_x, p1.m_y + p2.m_y);
}

class Math {
    void test() {
        Point point;
        point.m_x = 20;
        point.m_y = 30;
    }
    
    static void test2() {
        Point point;
        point.m_x = 20;
        point.m_y = 30;
    }
};

内部类

class Point {
private:
    int m_x;
    int m_y;
    static int ms_test2;
    
    static void test1() { }
    
public:
    Point() {}
    Point(int x, int y): m_x(x), m_y(y) {}
    
    class Math {
        void test3() {
            test1();
            ms_test2 = 10;
            
            Point point;
            point.m_y = 20;
            point.m_x = 10;
        }
    };
};

局部类

void test() {
    static int s_age = 0;
    int age = 0;
    
    class Point {
        int m_x;
        int m_y;
    public:
        static void display() {
            s_age = 20;
            // age = 30; // 错误
        }
    };
    
    Point::display();
}

类型转换

class Person {
public:
    int m_age = 0;
    virtual void run() {}
};

class Student: public Person { };

class Car {};

Student *stu1 = dynamic_cast<Student *>(p1); // NULL
Student *stu2 = dynamic_cast<Student *>(p3);
Car *car = dynamic_cast<Car *>(p1); // NULL
Student *stu3 = static_cast<Student *>(p1); // NULL
Student *stu4 = static_cast<Student *>(p3);

//    Car *car = static_cast<Car*>(p1); // 错误
Student *stu5 = reinterpret_cast<Student *>(p1);
Student *stu6 = reinterpret_cast<Student *>(p3);

Car *car1 = reinterpret_cast<Car*>(p1);

int *p = reinterpret_cast<int *>(100);
int num = reinterpret_cast<long>(p);

int i = 10;
double d1 = reinterpret_cast<double &>(i);

运算符重载


class Point {
public:
    int m_x;
    int m_y;
    
    Point(int x, int y);
    Point(const Point &point);
    
    Point operator+(const Point &p1) const;
    Point operator-(const Point &p1) const;
    const Point operator-() const;
    Point &operator+=(const Point &point);
    Point &operator-=(const Point &point);
    bool operator==(const Point &point);
    bool operator!=(const Point &point);
  // 前置递增返回引用
    Point &operator++();
    // 后置递增返回值
    const Point operator++(int);
   Point &operator--();
   const Point operator--(int);
   Point &operator=(const Point &);
   // 函数调用符重载 - 仿函数
   Point &operator()(int, int);
};


Point::Point(int x, int y): m_x(x), m_y(y) {}
Point::Point(const Point &point): m_x(point.m_x), m_y(point.m_y) {}

// 前++: 先加再用
Point &Point::operator++() {
    m_x++;
    m_y++;
    return *this;
}

// 后++:先用再加
const Point Point::operator++(int) {
    Point point(m_x, m_y);
    m_x++;
    m_y++;
    return point;
}

Point Point::operator+(const Point &point) const {
    return Point(m_x + point.m_x, m_y + point.m_y);
}

Point Point::operator-(const Point &p1) const {
    return Point(m_x - p1.m_x, m_y - p1.m_y);
}

const Point Point::operator-() const {
    return Point(-m_x, -m_y);
}

Point &Point::operator+=(const Point &point) {
    m_x += point.m_x;
    m_y += point.m_y;
    return *this;
}

Point &Point::operator-=(const Point &point) {
    m_x -= point.m_x;
    m_y -= point.m_y;
    return *this;
}

bool Point::operator==(const Point &point) {
    return m_x == point.m_x && m_y == point.m_y;
}

bool Point::operator!=(const Point &point) {
    return m_x != point.m_x && m_y != point.m_y;
}

Point & Point::operator--() {
   this->m_x = this->m_x - 1;
   this->m_y = this->m_y - 1;
   return  *this;
}

const Point Point::operator--(int) {
   Point temp = *this;
   m_x = m_x - 1;
   m_y = m_y - 1;
   return temp;
}

Point &Point::operator=(const Point & point) {
   // 应该先判断是否属性在堆区,如果有先释放干净,再进行深拷贝
   /**
    if (m_age != nullptr) {
       delete m_age;
       m_age = nullptr;
    }
    */
   cout << m_x << m_y << endl;
   m_x = point.m_x;
   m_y = point.m_y;
   
   return *this;
}

Point &Point::operator()(int x, int y) {
   m_x = x;
   m_y = y;
   return *this;
}

class Person {
public:
    int m_age = 0;
    
    Person &operator=(const Person &person) {
        this-> m_age = person.m_age;
        return *this;
    }
};

class Student: public Person {
public:
    int m_score;
    
    Student &operator=(const Student &student) {
        Person::operator=(student);
        this->m_score = student.m_score;
        return *this;
    }
};

仿函数

class Sum {
public:
   int operator()(int a, int b) {
       return a + b;
    }
};

void testSum() {
    Sum sum;
    sum(1, 2);
}

运算符重载注意点

上一篇下一篇

猜你喜欢

热点阅读