const经典例子小结

2014-11-23 19:04:15 · 作者: · 浏览: 13

在看这个例子之前,可以先看看:详解C和C++中的const和const和static变量的存放位置这样将会对const有非常全面的了解和认识:

下面我们将通过下面的例子看看const的一些非常不易发觉的错误:

#include
  
   
using namespace std;

class String
{
public:
	friend ostream& operator<<(ostream& os,const String& str);
    String(char* pstring):pstring_(pstring){ }
	void display(){pstring_ = "hello";cout << pstring_ << endl;}
    char& operator[](int index)const
	{
		return pstring_[index];
	}        
private:
    char* pstring_;        
};

ostream& operator<<(ostream& os,const String& str)
{
	os << str.pstring_;
	return os;
}

void f( const String&  str)
{
	const_cast
   
    (str).display(); } void g(String& const str) { String a("world"); str = a; } //void h(String* const str){} int main() { char a[] = "hello"; const String str(a); String ptr("nihao"); //const String str("how are you "); char& c=str[0]; //call const-function c='B'; //modified 'pstring_ ' from outer cout << str << endl; f(str); g(ptr); return 0; } 
   
  

 
 

大家看下面三条语句:

char a[] = "hello";
const String str(a);
char& c=str[0]; //call const-function
c='B'; //modified 'pstring_ ' from outer

我们本来定义了一个常量的String,但是通过这种间接的方法却能够去修改它,为什么呢?然后我们如何避免这种现象呢?

我们的方法就是将返回类型改为常量类型,即:

const char& operator[](int index)const;

这样就保证了类型上的一致。但是,即使返回类型不改为const,那么下面的为什么又不行呢:

const String str("how are you ");
char& c=str[0]; //call const-function
c='B'; //modified 'pstring_ ' from outer

我们这里就必须要弄清楚一件事情,就是对于一个字符串来说,它是保存在常量区的,这里面的数据时不能更改的,此时"how are you "保存在常量区(只读存储区),而将这个字符串初始化给对象str的时候,相当于是将这个字符串的地址赋给对象的变量pstring_,此时,两者将指向同一个字符串,因此里面的数据是无法更改的。而前面的将一个字符串赋给一个数组则是将其的一个副本附给数组,而这个数组存储在栈中,是能够修改的。

对于函数

void f( const String& str)
{
const_cast (str).display();
}

我们可知参数中的const是多余的,因为函数体中又将其强制转换成了非常量变量。

而对于函数g和h参数中的const不仅是无用的,而且也是非法的,因为此时的const修饰的是形参,而不是实参,他们所指向的对象仍然是可以修改的,只能讲非常量参数传递给他们,并且不能被重置。例如:

g(ptr);这条语句之后,ptr的值将会改变成world。