class Same {
public:
    void reverse(const char* str)
    {
        char* A = const_cast<char*>(str);
        A[0] = '1';

        printf("%u\n",A);
        printf("%u\n",str);
        return;
    }
};


int main()
{
    Same re;
    string str = "This is nowcoder";
    printf("%u\n", &str);
    re.reverse(str.data());
    cout<< str << "  " << endl;
    return 0;
}

为什么str的值被修改了呢?

posted on 2019-08-18 18:07  bluesky0318  阅读(311)  评论(0编辑  收藏  举报