美文网首页
C++ 友元,内部类,运算符重载

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

作者: lieon | 来源:发表于2020-11-10 11:08 被阅读0次

友元

  • 友元包括友元函数和友元类
  • 如果将函数A(非成员函数)声明为类C的友元函数,那么函数A就能直接访问类C对象的所有成员
  • 如果将类A声明为类C的友元类,那么类A的所有成员函数就能直接访问类C对象的所有成员
  • 友元破坏了面向对象的封装性,但在某些频繁访问成员变量的地方可以提高性能
  • 朋友元的就是让一个函数或者类,访问另一个类中的私有成员
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;
    }
};

内部类

  • 如果将类A定义在类C的内部。那么类A就是一个内部类(嵌套类)
  • 内部类的特点
    • 支持public, protected,private权限
    • 成员函数可以直接不带类名,对象名访问其外部类的static成员
    • 成员函数可以直接访问其外部类对象的所有成员(反过来则不行)
    • 不会影响外部内的内存布局
    • 可以在外部类内部声明,在外部类外面进行定义
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;
        }
    };
};

局部类

  • 在一个函数内部定义的类,称为局部类
  • 局部类的特点
    • 作用域仅限于所在的函数内部
    • 其所有成员必须定义在类内部,不允许定义static成员变量
    • 成员函数不能直接访问函数的局部变量(static变量除外)
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();
}

类型转换

  • C++中建议使用C++的类型转换符取代C风格的强制类型装换

  • C++中有4个类型转换符: static_cast, dynamin_cast, reinterpret_cast, const_cast

  • const_cast: 一般用于去掉const属性,将const转换成非const

     const Person *p1 = new Person();
     // p1->m_age = 20; 错误
     
     Person *p2 = const_cast<Person*>(p1);
     p2->m_age = 20;
    
  • dynamic_cast:一般用于多态类型的转换,有运行时安全检测

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
  • static_cast
    • 对比dynamic_cast,缺乏运行时安全检测
    • 不能交叉装换(不是统一继承体系的,无法转换)
    • 常用用于基础数据类型的转换,非const转成const
Student *stu3 = static_cast<Student *>(p1); // NULL
Student *stu4 = static_cast<Student *>(p3);

//    Car *car = static_cast<Car*>(p1); // 错误
  • reinterpret_cast
    • 属于比较底层的强制转换,没有任何类型检查和格式转换
    • 可以交叉转换
    • 可以将指针和整数互相转换
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);
}

运算符重载注意点

  • 有些运算符不可以被重载,比如:
    • 对象成员访问运算符: .
    • 域运算符号: ::
    • 三目运算符: ? :
    • sizeof
  • 有些运算符只能重载为成员函数,比如:
    • 赋值运算符: =
    • 下标运算符:[]
    • 函数运算符: ()
    • 指针访问成员: ->

相关文章

  • 11. 匿名对象、隐式构造、默认构造函数

    一.匿名对象 二.隐士构造函数 三. 四,友元函数 五.内部类 六.运算符重载

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

    友元 友元包括友元函数和友元类 如果将函数A(非成员函数)声明为类C的友元函数,那么函数A就能直接访问类C对象的所...

  • 1.2.17_C++ ++ 和 -- 运算符重载

    C++ 重载运算符和重载函数 递增运算符( ++ )和递减运算符( -- )是 C++ 语言中两个重要的一元运算符...

  • C++ 部分运算符重载

    可重载的运算符 不可重载的运算符和符号 重载运算符为类的成员函数 重载运算符为友元函数 重载赋值运算符 重载流插入...

  • 1.2.14_C++ 二元运算符重载

    C++ 重载运算符和重载函数 二元运算符需要两个参数,下面是二元运算符的实例。 我们平常使用的加运算符( + )、...

  • 1.2.13_C++ 一元运算符重载

    C++ 重载运算符和重载函数 一元运算符只对一个操作数进行操作,下面是一元运算符的实例: 递增运算符( ++ )和...

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

  • 13. C++基本运算符重载

    基本上我们进行运算符重载时有两种形式,类内的运算符重载和顶层函数位置的运算符重载。 操作符重载指的是将C++提供的...

  • 1.2.15_C++ 关系运算符重载

    C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),...

  • C++入门笔记 OOP与类3

    运算符重载 在C++中,运算符的重载可以看作一种特殊的函数。一元运算符有一个参数;二元运算符有两个参数。对于二元运...

网友评论

      本文标题:C++ 友元,内部类,运算符重载

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