c++ special member functions- const and volatile members

3901 ワード


what are the special functions, the special function includes the following. 
 
 
  • constructor - initialization
  • destructor  - destruction
  • assignment
  • memmory management
  • type conversion

  •  
    what we are going to do discuss here is the const and volatile member function.
     
    Const member:
     
    In general , any class that is spected to be used extensivly should declare the member functions that do not modify the class data members as const members. 
     
     
    By const member does not change the class members. That is only half the truth, the user has to be vigilant because a constant member does not guarantee  that everything to which the classs object referes will remain invariant throughtout he invocation of the meber function.
     
    First let' see the following class definition.
     
     
    /**
    * In general , any class that is spected to be used extensivly should declare the member functions that do not modify the class data members as const members. 
    */
    class Text
    {
    public:
    	void bad(const string &param) const; 
    private:
    	char * _text;
    };

     
    and then let's see the code as below., which shows that the C++ compiler does not detect the folowing violation because of pointer semantic.
     
     
    /**
    * though you cannot directly assign some value to class members, but the compiler is not able to guarantee that everything  to which the class object referes will remain invariant throughout the 
    * invocation of hte member function
    */
    
    void Text::bad(const string &param) const 
    {
    	_text = param.c_str(); // error: _text cannot be modified
    
    	for (int ix = 0; ix < param.size(); ++ix) { 
    		_text[ix] = param[ix]; // bad style but not an error 
    	}
    }
     
     
    Let's see how you normally use the const mebers...
     
     
     
    class Screen {
    public:
    	bool isEqual(char ch)  const; 
    
    	/** overload of the const and non-const mmber functions 
    	*
    	*/
    	char get(int x, int y);
    	char get(int x, int y) const;
    };
     
     
    and only the const class object can invoke the const members... here is the code. 
     
     
    void test_const_members() 
    {
    	const Screen cs;
    	Screen s;
    
    	char ch = cs.get(0, 0); // calls the const membres
    	ch = s.get(0, 0);       // calls the non-const members
    }

     
     
    volatile members
     
    it is less common that you will use this, but a member  or a constructor a destructor can be declared as  teh volatile member function. 
     
    as with the const members, only volatile class object can access the volatile members. 
     
    let's see an example. 
     
     
    class Screen {
    public:
    
           /*
    	* const member is easy to understand , but what about the volatile members
    	* 
    	* A class object is declared volatile if its value can possibly be changed in ways outside eithe rthe control or detection of the compilers (for example),
    	* only volatile members functions, constructors, and the destructor can be inovked by a volatile class object.
    	*/
    	void poll() volatile;
    };
     
    below is the code that test you how to use the volatile member function. 
     
     
     
    void test_volatile_member()
    {
    	volatile Screen vs;
    
    	vs.poll();
    
    	vs.isEqual('a'); // error:  you can only call volatile member function by a volatile clas object
    }