当前位置: 首页 > article >正文

houjie-cpp面向对象

houjie 面向对象

面向对象(上)

const

在一个函数后面放const,这个只能修饰成员函数,告诉编译器这个成员函数不会改数据

alt

const还是属于函数签名的一部分。 引用计数:涉及到共享的东东,然后当某个修改的时候,使用COW(Copy on Write)

new 和 delete

接管内存分配与回收 alt

  • 重载member operator new/delete alt

  • 重载成员操作符 new[] / delete[]

alt
alt

面向对象(下)

(3) non-explicit-one-argument ctor

#include<iostream>
using namespace std;


class Fraction{

public:
    Fraction(int num, int den = 1):m_numerator(num), m_denominator(den){

    }

    Fraction operator+(const Fraction& fra){
        //todo 需要修改规则
        return Fraction(this->m_numerator+fra.m_numerator,
        this->m_denominator + fra.m_denominator);
    }

    // operator double() const{
    //     //  error: use of overloaded operator '+' is ambiguous (with operand types 'Fraction' and 'int')
    //      因为这里存在两条分支路可以编译代码
    //     return (double)(m_numerator / m_denominator);
    // }

    int get_m_numberator() const {
        return this->m_numerator;
    }   

    int get_m_denominator() const{
        return this->m_denominator;
    }

private:
    int m_numerator;
    int m_denominator;
};



inline std::ostream& operator<< (std::ostream& os, const Fraction& o){
    return os << "(" << o.get_m_numberator() << ", " << o.get_m_denominator() << ")" << endl;
}

int main(){
    Fraction f(3,5);
    Fraction d2 = f + 4;    // 这里会把4转换为Fraction(4, 1)
    cout << d2 << endl
    return 0;
}

explitcit-one-argument ctor 给Fraction 加上explicit,会要求编译器不用把4强制转换为Fraction

conversion function 转换函数 没有返回类型,返回类型就是operator double()

转换函数
转换函数

(4)pointer-like classes 智能指针

#include<iostream>
using namespace std;

template<class T>
class my_shared_ptr{

public:
    T& operator*() const{
        return *px;
    }

    T* operator->() const{
        return px;
    }

    my_shared_ptr(T* p):px(p){

    }
private:
    T* px;
    long* pn;
};

struct Foo{
  void method(void){
    cout << "call Foo.method" << endl;
  }  
};


int main(){
    my_shared_ptr<Foo> sp(new Foo);
    Foo f(*sp);
    sp->method();   // -> 函数返回一个px指针,但是->可以继续使用所以等价于 px->method()
    return 0;
}

迭代器也是智能指针

迭代器
迭代器
迭代器的两个主要函数实现
迭代器的两个主要函数实现

function-like classes 仿函数

只要看到class里面有opearator()

仿函数
仿函数
标准库中的使用
标准库中的使用

对于一个类有两种方法like:可以做得像pointer、也可以弄成function

7 class Template:类模板

类模板
类模板

8 function template,函数模板

function template
function template

9 member template,成员模板

member template
member template
类模板例子
类模板例子

10 模版特化

模版特化 alt

11 模版偏特化

alt
alt

12 模板模板参数

alt

shared_prt 和 auto_ptr alt

13 关于 C++ 标准库

把C++容器和几百个algorith 使用一遍 学习过。

alt

14 三个主题 C++ 11

  1. 数量不定的模板参数 alt

区分一个和一包, 想知道后面args有多少个参数,就需要sizeof...(args)

  1. auto

  2. range-base for

alt

15 引用

变量有三种:value、pointer、reference

int* : pointer 2 integer int& : reference 2 interger

reference 一定要有初值,指针可以变化,reference 不可以变化

alt
alt

16 复合 & 继承

面向对象——class 和 class 的关系

  1. inheritance 继承关系下的构造和析构

构造的时候由内向外、析构的时候有外向内; alt

  1. composition 复合关系下的构造和析构
alt
  1. Inheritance + composition关系下的析构和构造
alt

17 关于vptr 和 vtbl

alt

静态绑定 和 动态绑定 动态绑定是先通过指针找到vptr 在找到vtbl,在去 d 调用函数 这是一种虚机制。 指针调用,向上转型,调用虚函数

18 关于 this

模板方法模式,发现 this 的应用 alt

19 关于动态绑定

通过点语法调用函数,只是一种静态调用 alt

本文由 mdnice 多平台发布


http://www.kler.cn/news/9968.html

相关文章:

  • 刷题day54:柱形图中最大矩形
  • Java多线程基础面试总结(一)
  • 【数据挖掘与商务智能决策】第十一章 AdaBoost与GBDT模型
  • 数字孪生智慧应急怎么实现?
  • 运行时内存数据区之虚拟机栈——操作数栈
  • 你真正了解低代码么?(国内低代码平台状况分析)
  • 国家出手管人工智能AI了
  • Python数据分析案例24——基于深度学习的锂电池寿命预测
  • Difference between HTTP1.0 and HTTP1.1
  • Spring 之依赖注入底层原理
  • like字符通配符%_、查询空值、去重、and、or、MySQL数据库 - 单表查询(二)(头歌实践教学平台)
  • 【数据结构】栈各个接口的实现
  • 详解AUTOSAR:Green Hills Software(GHS)集成DaVinci Configurator生成的代码(RH850)(环境配置篇—1)
  • springboot+vue学生选课管理系统
  • 循环依赖详解及解决方案
  • 闭包和继承
  • 程序员为了女朋you进了华为,同学去了阿里,2年后对比收入懵了
  • GDPU C语言 天码行空7
  • 代码随想录算法训练营第五十五天 | 392. 判断子序列、115. 不同的子序列
  • 【grafana】使用多级变量解决Granfana模板变量中的大小限制
  • RHCE——shell脚本练习
  • DC 使用记录
  • 一次性搞懂dBSPL、dBm、dBu、dBV、dBFS的区别!
  • 谈ChatGPT基本信息
  • Mac平台上有哪些好用的常用软件?
  • 软件重构方法
  • Nacos 性能报告
  • 2023-04-14 lua + C动态库交叉debug
  • 逆向入门--何为OEP
  • 故障注入的方法与工具