override,final的使用,两者都是针对虚函数,也就是说要有virtual关键字

简介:  1.override,final的使用,两者都是针对虚函数,也就是说要有virtual关键字 #include <iostream>   //C++中的final和override主要是针对虚函数 //加了final表示拒绝重写,这是为了使某些情况下,拒绝接口的重写 //override,声明重写父类的方法,前提是要有virtual关


1.override,final的使用,两者都是针对虚函数,也就是说要有virtual关键字

#include <iostream>

 

//C++中的finaloverride主要是针对虚函数

//加了final表示拒绝重写,这是为了使某些情况下,拒绝接口的重写

//override,声明重写父类的方法,前提是要有virtual关键字修饰函数。

//当父类没有接口,会提示错误

class ye

{

public:

    //final修饰的函数必须是虚函数,通过这种方式避免重写了一些老的接口

    virtual void print() final //加了final表示虚函数无法重写了

    {

        std::cout << "爷爷\n";

    }

    virtual void run()

    {

        std::cout << "爷爷run\n";

    }

};

 

class ba :public ye

{

public:

    void run() override

    {

        std::cout << "爸爸run\n";

    }

};

 

void main()

{

    ba ba1;

    ba1.run();  //结果是:爸爸run

 

    std::cin.get();

}

2.RTTI 实时类类型检测

#include <iostream>

//rtti,实时类类型检测

//typeid,dynamic_cat必须依赖于虚函数表

//类型不匹配转换失败,返回为空,类型安全

 

//成员变量的覆盖

//虚函数有虚函数表确定数据类型

 

class A

{

public:

    int num;

    static int data;

    virtual void run()

    {

        std::cout << "A run \n";

    }

};

 

//A中的静态变量进行初始化

int A::data = 1;

class B :public A

{

public:

    int num = 0;

    static int data;

    void run()

    {

        std::cout << "B run\n";

    }

    void test()

    {

        std::cout << num << "\n";

        std::cout << "B test\n";

    }

};

 

int B::data = 2;

 

void main()

{

    A a1;

    B b1;

    A *p1 = &a1;

    A *p2 = &b1;

    B *p3(nullptr);

    //p3 = static_cast<B *>(p1);//直接赋地址,不安全,与虚函数无关

    p3 = reinterpret_cast<B *>(p2);

    std::cout << p3 << "\n";

    p3->test();

 

    std::cin.get();

    //此次运行结果:

    //008FFE70

    //0

    //B test

}

3.C函数指针复习

#include <stdlib.h>

 

int add(int a,int b)

{

    return a + b;

}

 

void run()

{

    printf("\nrun");

}

 

void main()

{

    int(*p)(int, int) = add;//指向add的函数指针

    void(*p1)() = run;//指向run的函数指针

    printf("%d\n",p(1,2));

    printf("%d\n",(*p)(1,2)); //*p编译器自动将*p解释为p

    printf("%d\n",(******p)(1,2));//*p编译器自动将*p解释为p

 

    printf("%d\n", (&(**p))(1, 2)); //&没有*不可以执行,超过两个地址就不可以

    //&p不能,

    //printf("%d\n", (&(p))(1, 2));

    printf("%p,%p,%p", &p, *p, p);

    printf("\n%p,%p,%p", &p1, *p1, p1);

    //取地址,就是CPU即将调用函数执行

 

    getchar();

}

运行结果:

4.CPP函数指针

#include <iostream>

#include <stdlib.h>

 

void add(int a,int b)

{

    std::cout << a + b << std::endl;

}

 

void main()

{

    void(*p)(int, int) = add;

    p(1, 2);

    (*p)(1,2); //函数指针,会被当做指针来处理,*pp效果一样

    (*************p)(1,2);//函数指针,会被当做指针来处理,*pp效果一样

    (*&p)(1,2);

    (*******&p)(1,2);

    std::cout << (void *)p << "  " << (void *)(*p) << std::endl;

    std::cout << typeid(p).name() << std::endl;

    std::cout << typeid(*p).name() << std::endl;

    std::cout << typeid(&p).name() << std::endl;

    std::cout << typeid(*&p).name() << std::endl;

    //C++编译器会自动将*p处理为p

    std::cin.get();

}

运行结果:

5.类成员函数指针数组

#include <iostream>

#include <stdio.h>

 

//类成员函数指针,类成员函数指针数组,类成员二级函数指针

class com

{

private:

    int a;

    int b;

public:

    com(int x, int y) :a(x), b(y)

    {

    }

    int  jia(int a, int b)

    {

        return a + b;

    }

    int  jian(int a, int b)

    {

        return a - b;

    }

    int  cheng(int a, int b)

    {

        return a * b;

    }

    int  chu(int a, int b)

    {

        return a / b;

    }

};

 

void main()

{

    com com1(100, 20);

    auto fun1 = &com::jia;

    int(com::*p)(int, int) = &com::jia;  //注意这里要加上对象名

    std::cout << (com1.*p)(10, 20) << std::endl;//引用对象,类成员函数指针

    std::cout << typeid(p).name() << std::endl;

    std::cout << typeid(fun1).name() << std::endl;

 

    std::cin.get();

}

5.CPP函数指针数组

#include <iostream>

#include <stdio.h>

 

//类成员函数指针,类成员函数指针数组,类成员二级函数指针

class com

{

private:

    int a;

    int b;

public:

    com(int x, int y) :a(x), b(y)

    {

    }

    int  jia(int a, int b)

    {

        return a + b;

    }

    int  jian(int a, int b)

    {

        return a - b;

    }

    int  cheng(int a, int b)

    {

        return a * b;

    }

    int  chu(int a, int b)

    {

        return a / b;

    }

};

 

typedef int(com::*P)(int, int);//为函数指针定义别名p

void main()

{

    com com1(100, 200);

    //下面这种方式也是没有错的

    //P fun1[4] = { &com::jia, &com::jian, &com::cheng,&com::chu };

    //类成员函数指针数组

    int(com::*fun1[4])(int, int) = { &com::jia, &com::jian, &com::cheng, &com::chu };

    for (int i = 0; i < 4; i++)

    {

        std::cout << (com1.*fun1[i])(10, 20) << std::endl;

    }

    int(com::**funp)(int, int) = fun1;//指向类成员函数指针的指针

    for (; funp < fun1 + 4;funp++)

    {

        std::cout << (com1.**funp)(10, 20) << std::endl;

        printf("%p",*funp);

    }

 

    for (int i = 0; i < 4;i++)

    {

        auto func = fun1[i];

        std::cout << typeid(func).name() << std::endl;

        printf("%p", func);

    }

    std::cin.get();

}

运行结果:

6.高级new对象

#include<iostream>

 

class myclass

{

public:

    myclass()

    {

        std::cout << "创建\n";

    }

    ~myclass()

    {

        std::cout << "销毁\n";

    }

};

 

void main()

{

    char *pcathe = new char[1024];

    char *pcatheend = pcathe + 1024;

    std::cout << (void*)pcathe << "   " << (void*)pcatheend << std::endl;

 

    myclass *p = new(pcathe)myclass[10];//限定区域分配内存,覆盖模式

    std::cout << p << std::endl;

 

    //delete[] p;一般不需要delete.自动覆盖

    std::cout << p << std::endl;

 

    p = new(pcathe)myclass[10];

    std::cout << p << std::endl;

 

    //delete[] p;//只能释放一次

    std::cout << p << std::endl;

 

    /*

    myclass *pclass = new  myclass[10];

    std::cout << pclass <<std::endl;

    delete []pclass;

    pclass = NULL;

    std::cout << pclass <<std::endl;

    pclass = new myclass[10];

    std::cout << pclass <<std::endl;

    delete [] pclass;

    std::cout << pclass <<std::endl;

    */

 

    std::cin.get();

}

目录
相关文章
|
4月前
|
消息中间件 Kubernetes NoSQL
c++11 关键字 override 与 final
c++11 关键字 override 与 final
|
6月前
|
存储 Cloud Native Linux
C++ 关键字override,final的作用
C++ 关键字override,final的作用
|
5月前
|
程序员
虚函数的修饰符:final override(仅能虚函数)
虚函数的修饰符:final override(仅能虚函数)
30 0
|
8月前
|
Java 编译器 C#
C#中的override和new关键字
在 C# 中,派生类可以包含与基类方法同名的方法。 基类方法必须定义为 virtual。 如果派生类中的方法前面没有 new 或 override 关键字,则编译器将发出警告,该方法将有如存在 new 关键字一样执行操作。 如果派生类中的方法前面带有 new 关键字,则该方法被定义为独立于基类中的方法。 如果派生类中的方法前面带有 override 关键字,则派生类的对象将调用该方法,而不是调用基类方法。 可以从派生类中使用 base 关键字调用基类方法。
37 1
|
12月前
|
C# C++
virtual和abstract关键字
virtual和abstract关键字
88 0
Java常用关键字:this、super、final、static、访问修饰符
我从工作开始之前开始写博客,写到现在发现以前写的内容在现在看来有了更多想法,因此有了现在的知识重写计划,最主要的目的是维护github上的知识体系,让JavaStarter更加成熟。