zoukankan      html  css  js  c++  java
  • 特殊成员函数

    静态成员函数:
    1、成员函数也可以定义成静态的,与静态成员变量一样,系统对每个类只建立一个函数实体,该实体为该类的所有对象共享;
    2、静态成员函数体内不能使用非静态的成员变量和非静态的成员函数;只能调用静态成员数据和函数;
    3、静态成员函数的参数列表中不含有this指针,所以要在静态成员函数里使用非静态成员,必须给静态成员函数传参,传递类对象,通过对象调用其他非静态成员;

    #include <iostream>
    #include <string.h>
    using namespace std;
    class computer
    {
    private:
            char *brand;
            float price1;
            static float price;
            static void printprice()
            {
                    cout<<"总价格:"<<price<<endl;
            }
    public:
            computer(const char* brand1,float price2)
            :price1(price2)
            {
                    cout<<"computer(const char*,float)"<<endl;
                    brand=new char[strlen(brand1)+1];
                    strcpy(brand,brand1);
                    price+=price1;
            }
            ~computer()
            {
                    cout<<"~computer()"<<endl;
                    delete []brand;
                    price-=price1;
            }
            static void print(computer &com)   
     //函数只能访问静态成员,要向访问其他就只能传参来实现
            {
                    cout<<"品牌:"<<com.brand<<endl;
                    cout<<"价格:"<<com.price1<<endl;
                    printprice();
            }
    };

    float computer::price=0.0;
    int main()
    {
            computer p1("IBM",7000);  
    //print()是静态成员,不在某一个对象里面,不能对象调用
            computer::print(p1);
            cout<<endl;
            {
                    computer p2("MAC",10000);
                    computer::print(p2);
            } //函数体完会自动调用析构函数
            cout<<endl;

            computer::print(p1);
            return 0;
    }



    const成员函数:
    1、只能读取类数据成员,而不能修改
    2、只能调用const成员函数,不能调用非const成员函数
    3、使用const对象,都是去调用const成员函数
    4、非const对象,可以使用const成员函数,但是const对象不能

    类内定义时:
    类型 函数名(参数列表) const
    {
            函数体
    }
    类外定义时:
    类内声明:
    类型 函数名(参数列表) const;
    类外定义:
    类型 类名::函数名(参数列表) const
    {
            函数体
    }



    #include <iostream>
    using namespace std;
    class point
    {
            private:
                    int ix;
                    int iy;
            public:
                    point (int ix1=0,int iy1=0)
                    :ix(ix1)
                    ,iy(iy1)
                    {
                            cout<<"point(int,int)"<<endl;
                    }
                    void set_ix(int x)
                    {
                            ix=x;
                    }
                    void print()   //和下面函数构成重载
                    {
                            cout<<"print()"<<endl;
                            cout<<"("<<ix<<","<<iy<<")"<<endl;
                    }
                    void print()const
                    {
                            //void set_ix(int x)   不能调用非const成员函数
                            cout<<"print()const"<<endl;
                            cout<<"("<<ix<<","<<iy<<")"<<endl;
                    }
    };
    int main()
    {
            point p1(1,2);
            p1.print();
     //可以调用普通print,也可以调用const print,显示定义了普通print,优先调用普通print
            cout<<endl;

            p1.set_ix(2);
            p1.print();
            cout<<endl;

            const point p2(3,4);
            p2.print();
            //p2.set_ix(5); //const对象不能调用非const成员函数
    }


  • 相关阅读:
    CCS中cmd文件的编写
    makefile与动态链接库案例分析——动态库链接动态库
    LD的-rpath,-rpath-link
    动态库的链接和链接选项-L,-rpath-link,-rpath
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
    使用插件扩展Docker
    Docker 的插件式设计
    关于Docker目录挂载的总结(转)
    服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
    转:基于Jmeter的MQTT测试插件
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8804779.html
Copyright © 2011-2022 走看看