为什么这个const成员函数允许一个成员变量被修改? [英] Why does this const member function allow a member variable to be modified?

查看:191
本文介绍了为什么这个const成员函数允许一个成员变量被修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class String 
{

private:
char * rep;

public:
String(const char *);
void toUpper()const;
};


String :: String(const char * s)
{
rep = new char [strlen(s)+1];
strcpy(rep,s);
}


void String :: toUpper()const
{
for(int i = 0; rep [i]; i ++)
rep [i] = toupper(rep [i]);
}


int main()
{
const String lower(lower);
lower.toUpper();

cout<<下<< endl;
return 0;
}


解决方案

函数,是不会改变其成员变量的成员函数。

成员函数上的const不意味着const char *。



您的示例不会改变成员变量本身。



成员函数上的一个const,将确保将所有成员变量视为const。



这意味着如果你有:

  int x; 
char c;
char * p;

然后您将拥有:

  const int x; 
const char c;
char * const p; //< - 表示你不能改变p指向,但你可以改变数据p点到

有两种类型的常量指针。一个const成员函数使用我上面列出的一个。






获取所需错误的方法



尝试更改:

  

到:

 code> char rep [1024]; 

并删除此行:

  rep = new char [strlen(s)+1]; 

它会抛出你期望的错误(不能修改成员,因为const关键字) p>

因为只有一种类型的const数组。这意味着你不能修改任何数据。






现在整个系统实际上已经破坏了以下示例:

  class String 
{

private:
char rep2 [1024]
char * rep;

...


String :: String(const char * s)
{
rep = rep2;
strcpy(rep,s);
}

所以这里学到的教训是函数不能确保你的对象不会改变。



它只保证每个成员变量都被视为const。对于指针,const char *和char * const之间有一个很大的差异。



大多数时候,一个const成员函数意味着成员函数不会修改对象本身,但这并不总是这样,如上面的例子所示。


class String
{

    private:
        char* rep;

    public:
        String (const char*);
        void toUpper() const;
};


String :: String (const char* s)
{
    rep = new char [strlen(s)+1];
    strcpy (rep, s);
}


void String :: toUpper () const
{
    for (int i = 0; rep [i]; i++)
    rep[i] = toupper(rep[i]);
}


int main ()
{
    const String lower ("lower");
    lower.toUpper();

    cout << lower << endl;
    return 0;
}

解决方案

A const member function, is a member function that does not mutate its member variables.

const on a member function does not imply const char *. Which would mean that you can't change the data in the address the pointer holds.

Your example does not mutate the member variables themselves.

A const on a member function, will ensure that you treat all of your member variables as const.

That means if you have:

int x;
char c;
char *p;

Then you will have:

const int x;
const char c;
char * const p; //<-- means you cannot change what p points to, but you can change the data p points to

There are 2 types of const pointers. A const member function uses the one I've listed above.


A way to get the error you want:

Try changing:

char * rep;

to:

char rep[1024];

And remove this line:

rep = new char [strlen(s)+1];

It will throw the error you are expecting (can't modify members because of const keyword)

Because there is only 1 type of const array. And that means you cannot modify any of its data.


Now the whole system is actually broken with the following example:

class String
{

    private:
        char rep2[1024];
        char* rep;

 ...


 String :: String (const char* s)
 {
    rep = rep2;
    strcpy (rep, s); 
 }

So the lesson to learn here is that the const keyword on member functions does not ensure that your object will not change at all.

It only ensures that each member variable will be treated as const. And for pointers, there is a big diff between const char * and char * const.

Most of the time a const member function will mean that the member function will not modify the object itself, but this is not always the case, as the above example shows.

这篇关于为什么这个const成员函数允许一个成员变量被修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆