static member variable and static member function in a class

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 & 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;
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值