zoukankan      html  css  js  c++  java
  • c++学习-特殊类成员

    静态变量:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    
    public:
        
        A(){ total++; }
        static int total;
    
    };
    
    //@warn 静态成员变量必须在全局进行定义
    int A::total = 0; 
    
    void main()
    {
        A a1;
        A a2;
    
        cout << a1.total<< endl;
        cout << a2.total<< endl;
      cout << A::total << endl; }

    静态成员函数:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    
    public:
        A(){ total++; }
        static void show()
        {
            cout << total << endl;
        }
    
        void echo()
        {
            cout << total << endl;
        }
    
    private:
        static int total;
        int a;
    };
    
    int A::total = 10; 
    
    void main()
    {
        A::show();
    
        A a1;
        a1.echo();

    A a1,a2;
    a1.echo();
    a1.show();//也可以通过对象调用静态函数


    }

    继承后静态成员函数和方法仍旧可用:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    
    public:
        A(){ total++; }
        static void show()
        {
            cout << total << endl;
        }
    
        void echo()
        {
            cout << total << endl;
        }
    
    private:
        static int total;
        int a;
    };
    
    class B :public A{
    
    };
    
    int A::total = 10; 
    
    void main()
    {
        A::show();
        B::show();
    
    
    }

    静态成员函数不能说明为虚函数 

    int(*fun)(int);//声明一个函数指针,指向的函数的参数为int,返回值为int
    int*fun(int);//声明一个函数,函数的参数为int,返回值为int型指针

    函数指针使用:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    
    void main()
    {
        int(*p)(int, int);
        p = test1;
        cout << p(1, 2)<< endl;
    
        p = test2;
        cout << p(1, 2) << endl;
        
    
    }

    函数指针数组:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    
    void main()
    {
        int(*p[2])(int, int) = { test1, test2 };
    
        cout << p[0](1,2) << endl;
        cout << p[1](2, 3) << endl;
        
    
    }

    函数指针做参数:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    void show(int(*p)(int, int), int x,int y)
    {
        cout << p(x,y) << endl;
    }
    
    void main()
    {
        show(test1, 1,2);
        show(test2, 1, 2);
    }

    typedef来简化定义:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    typedef int(*p)(int, int); // p代表 int(*p)(int, int);
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    void show(p vp, int x,int y)
    {
        cout << vp(x, y) << endl;
    }
    
    void main()
    {
        show(test1, 1,2);
        show(test2, 1, 2);
    }

    成员函数指针:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    public:
        int show(int x, int y){ return x*y; }
    };
    
    int(A::*p)(int, int);
    
    void main()
    {
        p = &A::show;
    
        A a1;
        (a1.*p)(1, 23); //调用
    
    }

    多态和成员函数指针:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class human{
    public:
        virtual void say() = 0;
    };
    
    class father :public human{
    public:
        void say(){ cout << "father say:" << endl; }
    };
    
    class mother :public human{
    public:
        void say(){ cout << "mother say:" << endl; }
    };
    
    void main()
    {
        void (human::*fun)()=0;
        fun = &human::say;
    
        human *p;
        p = new father;
        (p->*fun)();
    
        p = new mother;
        (p->*fun)();
    
    }
  • 相关阅读:
    selenium 模拟键盘事件 复制粘贴、右键、回车等
    02安卓用户界面优化之(二)SlidingMenu使用方法
    02Android用户界面优化之(一)Android Fragment
    (九)Android权限系统
    Android SDK 在线更新镜像服务器资源
    (八)Android广播接收器BroadcastReceiver
    (七)Android中AIDL的应用与理解
    (六)Android中Service通信
    (五)认识Android中的Service
    Gradle中文乱码
  • 原文地址:https://www.cnblogs.com/siqi/p/4641249.html
Copyright © 2011-2022 走看看