zoukankan      html  css  js  c++  java
  • 类的常量成员

    重载= 和构造类似

    father s(0);

    father s=0;//定义的时候初始化 隐性调用构造函数  定义对象的时候直接赋值

    s=1;//调用重载=  定义对象之后 再对对象赋值

    1.c++的四个强转

    static_cast<类型>值 不需要做任何检查 

    const_cast const 引用  const指针 去掉变量边上的const   int *p = const_cast<int*>(&x);

    reinterpret_cast 指针和int都是四个字节 所以可以吧地址存放到int里面

    dynamic_cast 检查多态的时候  执政/引用转换的安全性

           父类地址 转换子类对象 有安全隐患 最后返回null

    父类指针可以指向子类函数 运用虚函数 间接的对子类对象的调用

    子类指针至下关父类对象 子类指针访问子类成员 不可访问父类成员 会有越界隐患 所以要用到强转

    2.类中成员变量和成员函数的访问方式

     静态编译 编译之前 确定调用什么函数 比较快 但不灵活

     动态编译 编译之前不知道调用什么函数 根据虚函数 对象中的虚表根据虚表去找函数调用 非常灵活 但是速度比较慢

    3.智能指针 头文件<memory>  智能的释放内存

    4.类中的const和static

    const 在类中 修饰参数 参数不能需改   修饰成员变量 通过初始化形参列表赋值  修饰成员变量 在成员函数后面家,表示这个成员函数不可修改,利用this指针寻找需要的值

    5.关于运算符重载的成员指针运算符 .*

    son.h文件

     1 #pragma once
     2 #include<iostream>
     3 using namespace std;
     4 class father
     5 {
     6 protected:
     7     int age;
     8     char name[20];
     9 public:
    10     father(){}
    11     //explicit 禁止隐式调用方式
    12     /*
    13     father s(1);显示调用
    14     father s=1;隐式调用    
    15     */
    16     father(int x)
    17     {
    18         age = x;
    19         cout << "调用了构造函数" << endl;
    20     }
    21     ~father(){
    22         cout << "调用了析构函数" << endl;
    23     }
    24         virtual void fun()
    25         {
    26         }
    27 
    28 };
    29 
    30 class son:public father
    31 {
    32 protected:
    33     int score;
    34 public:
    35     son();
    36     ~son();
    37     void fun()
    38     {}
    39     void fun2()
    40     {
    41         score = 1;//子类新加的函数用来修改子类成员
    42     }
    43     void fun2()const{
    44         this->score;
    45     }
    46 };

    类的常量成员.cpp

     1 #include<vector>
     2 #include<memory>//只能指针 会自动调用析构 但只能调用单个
     3 #include"son.h"
     4 template<class T>
     5 void fun(T a, T b)
     6 {
     7 
     8 }
     9 template<class T>
    10 class A
    11 {
    12 public:
    13     A();
    14     void fun();
    15 };
    16 template<class T>
    17 A<T>::A()
    18 {
    19 
    20 }
    21 template<class T>
    22 void A<T>::fun()
    23 {
    24 
    25 }
    26 int main()
    27 {
    28     //son*Q = dynamic_cast<son*>(new father);子类指针传递给父类对象要进行强转
    29     //cout<<Q<<endl;
    30     //Q->fun2();
    31     {
    32         auto_ptr<father> point(new father);
    33         //对象通过析构的时候调用
    34         /*
    35         可以选择使用这个智能指针 帮忙管理内存  对象生命周期结束的时候 自动回收内存
    36         只能指针只能管理一个一个对象 因为内部函数只有一个delete
    37         auto_ptr内部使用的delete 记得只申请一个
    38         */
    39 
    40     }
    41     fun<int>(3, 4);//显式调用 指明T的类型
    42     fun(3, 4);//隐式调用 根据函数参数确定模板函数T的类型
    43     A<int> a;//如果是模板类 必须指明参数类型
    44     vector<int> arr;
    45     arr.push_back(1);
    46     cout << arr[0] << endl;
    47     cout << static_cast<int>(3.14) << endl;
    48     const int x = 10;
    49     int *p = const_cast<int*>(&x);
    50     int y = reinterpret_cast<int>(&x);
    51     cin.get();
    52     return 0;
    53 
    54     cin.get();
    55     return 0;
    56 }
  • 相关阅读:
    华为 VRP系统管理
    清除远程桌面登陆记录
    windows多用户登陆
    破解win7系统密码
    ms10-061漏洞复现
    通达OA任意用户登录漏洞复现(POC+手工方式实现)
    Windows五次Shift漏洞
    CODESYS V3远程堆溢出漏洞复现(环境配置+复现过程)
    工控软件DLL劫持漏洞复现
    Redis远程命令执行漏洞复现
  • 原文地址:https://www.cnblogs.com/liugangjiayou/p/11397888.html
Copyright © 2011-2022 走看看