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

友元

  • 友元包括友元函数和友元类
  • 如果将函数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
  • 有些运算符只能重载为成员函数,比如:
    • 赋值运算符: =
    • 下标运算符:[]
    • 函数运算符: ()
    • 指针访问成员: ->
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章 计算机与C++编程简介 C++程序6个阶段编程 ->预处理->编译->连接->装入->执行1.程序在编译器...
    rogertan30阅读 4,095评论 0 1
  • 使用类 运算符重载 重载后的运算符必须至少有一个操作数是用户定义的类型 使用运算符时不能违反原来运算符的句法规则,...
    HYIndex阅读 407评论 0 1
  • 前言 最近发现要学习C++来开发NDK,不得不把基础的东西记录下来,否则学的太多会混淆,废话不多说,开始记录我的C...
    程序爱好者阅读 406评论 0 1
  • 前言 最近项目在开发涉及到的C++内容相对比较多,整理一下,过程中用到的C++面向对象的语法笔记 正文 知识点的概...
    JasonChen8888阅读 353评论 0 1
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,573评论 16 22