简体   繁体   English

有关const_cast <>的行为

[英]Behaviour about const_cast<>

I have written a small problem for checking the behavior of const_cast on const data member. 我写了一个小问题来检查const数据成员上const_cast的行为。

using namespace std;


     class myString{
         public:
                 myString(char * str)
                 {
                         p=str;
                 }
                 const char * getString(){
                         return p;
                 }
         private:
                 const char *p;
 } ;


int main()
{
        char *p=(char*)malloc(8);
        cin>>p;
        myString *m= new myString(p);
        char *s =const_cast<char*>(m->getString());
        s[6]='y';
        cout<<s<<endl;
        return 0;
}

After running this program I give the out as "yogendra" (a 8 letter string). 运行该程序后,我将其输出为“ yogendra”(8个字母的字符串)。 and i got the output as "yogendya" Now my doubt. 我得到的输出为“ yogendya”。现在我很怀疑。 Through const_cast<> we can override the behavior of the data member itself as here the string is const char* still after casting i can modify it. 通过const_cast <>,我们可以覆盖数据成员本身的行为,因为在转换后,字符串仍为const char *,我仍然可以对其进行修改。

You've described exactly what const_cast is for - it allows you to removed the const ness from something and modify it. 您已经准确地描述了const_cast的用途-它使您可以从某些内容中删除const并进行修改。 It's up to you not to abuse that power. 您有责任不滥用这种权力。 :-) :-)

(In your case this doesn't apply, but note that you can cause crashes by using const_cast - for example: (在您的情况下,这并不适用,但是请注意,使用const_cast可能会导致崩溃-例如:

const char *c = "Hello";
char *s = const_cast<char*>(c);
s[0] = 'h';

could crash because the compiler can put the string literal into read-only memory.) 可能会崩溃,因为编译器可以将字符串文字放入只读内存中。)

yes, you can use const_cast<> this way and it will not be an undefined behaviour since object pointed to by const char* in your class is indeed non-const of type char* . 是的,您可以以这种方式使用const_cast<> ,并且不会出现未定义的行为,因为您的类中const char*指向的对象确实是char*类型的非const。 but be careful:. 不过要小心:。 C++ standard. C ++标准。 §7.1.​5.1/4 says §7.1。5.1 / 4说

Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior 除了可以声明任何声明为可变的类成员(7.1.1)之外,任何在其生存期(3.8)内修改const对象的尝试都会导致未定义的行为

safe use of const_cast is to ie cast const from const reference to a non-const object: when there is non const object and you have const ref to it, you can cast const from this safely 安全使用const_cast是将const从const引用转换为非const对象:当存在非const对象并且您具有const ref时,可以从此安全地转换const

If you say 如果你说

char *s =const_cast<char*>(m->getString());

then you essentially remove the "const" from the pointer, and you declare your s to be a pointer to char, and it's a writable string. 然后从指针中删除“ const”,然后将s声明为指向char的指针,这是一个可写的字符串。 So the next line 所以下一行

s[6]='y';

is perfectly fine. 很好。 To keep the const you ought to declare s as a const pointer 要保留const,您应该将s声明为const指针

const char *s = m->getString();

in which case you won't be able to overwrite the constant string (error: assignment of read-only location). 在这种情况下,您将无法覆盖常量字符串(错误:分配只读位置)。 I assume that is what you want? 我以为那是你想要的? The const_cast will simply add/remove the const and in your case, remove it. const_cast将仅添加/删除const ,在您的情况下,将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM