您的位置:首页 > 移动开发 > Objective-C

static member variable and static member function in a class

2008-04-28 03:17 435 查看
static member variable

In order to share data in different objects, and not destroy the principle of data hidden, keep safe. So static member is shared by all the objects of a class, and not the member of a given object. And it can save memory, as it only save in one location to shared by all the objects. The member can also be updated, we can ensure that the objects read the updated value, it's worthy.

To use a static member, we should pay attenen to
1. use static keyword when in definition
2. there is different between static member initialization and common data member initialization
<data type> <classname>::<static member name>=<value>
it means,
Initialization should be out of class, no static or private/public here;
The static member should be initialized before using, we should use the style <class name>::<static member name> to initialize.
If the memeber is public, we chould modify the value as the format above.

static member function
The same as static member variable, both are not belong to object, but class. So, we don't need object name to reference. We could not get nonstatic member variable directly, but we could get static member variable directly. Static member function will not use the default this pointer, it's why it could not use the nonstatic member variable. Maybe in sometimes, we sould use static function to read nonstatic member variable. Maybe in multithread, in fact, I'm not familiar with that. We said static member function don't have this pointer, why not pass this pointer by ourselves? We could write a function to return the objects address(pay attention to object), and we will get what we want. It only simulate the behavior of pass this pointer.

We could use static member in order to
1. save the number of objects (it's useful sometimes, as you want to know how many objects is generated)
2. as a flag to show status (we could know how something is working now, as file is open or not, printer is using or not)
3. save the first or the last address in a linked list

A simple example to use static member variable and static function

 


// file: object.h




#pragma once




#include <iostream>




class Object




...{


public:


    Object()


        : name("")




    ...{


        number++;


    }




    Object(const std::string &name)


        : name(name)




    ...{


        number++; 


    }




    // this function is only to simulate this pointer


    const Object <
4000
/span>& pointer() const




    ...{


        return (*this);


    }




    // static function to read static variable


    static int getNumber()




    ...{


        return number;


    }




    // only to show how to read nonstatic variable in a static function


    static const std::string & getName(const Object & object)




    ...{


        return object.pointer().name;


    }




    


private:


    std::string name;




private:


    // set to private to prevent modified


    static int number;


};

 


// file: object.cpp




#include "Object.h"




int Object::number = 0;

 


// file: main.cpp




#include  <iostream>




#include "Object.h"




using namespace std;




int main (int argc, char * argv[])




...{


    Object a("vincent");


    cout<<a.getNumber()<<endl;


    Object c,d("Jim"),e,f;


    cout<<c.getNumber()<<endl;


    cout<<d.getNumber()<<endl;


    cout<<a.getNumber()<<endl;


    // a.getName(a) and b.getName(a) are both OK, but not recommended


    cout<<Object::getName(a).c_str()<<endl;


    system("pause");


    return 0;


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息