小编San*_*dze的帖子

C++ 字符串和 char* 的区别示例

这是来自 hackerrank“继承代码”的示例,

虽然这有效并what()返回n,但如果我注释返回what并取消注释当前注释的部分what()将返回垃圾。

它们在我看来都一样,有什么区别?

/* Define the exception here */
struct BadLengthException : public exception {      
    public: 
    int num;
    string stra;
    BadLengthException(int n){
        this->num = n;
        this->stra = to_string(this->num);
    };
    
   const char * what () const throw () {

       return this->stra.c_str();


       //string a = to_string(this->num);
       //return  a.c_str();
   }
};
Run Code Online (Sandbox Code Playgroud)

c++ pointers std c-strings char

1
推荐指数
1
解决办法
63
查看次数

标签 统计

c++ ×1

c-strings ×1

char ×1

pointers ×1

std ×1