c++ special member functions- const and volatile members

本文深入探讨了C++中常量成员函数与可变成员函数的概念、用法及限制,通过实例展示了如何正确使用这些特性以避免潜在的错误,并强调了常量与可变成员函数在不同场景下的应用与注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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
}
 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值