今天在做一個(gè)練習(xí)時(shí)突然被char str[]和char* str給迷住了,研究了半天才搞定

在c++中對(duì)char類型做了特殊處理,原因是兼容c語言
eg:
   char str[]="abc\0def";
   這里的str是一個(gè)地址,c++在運(yùn)行時(shí)會(huì)自動(dòng)將str的地址從str[0]一直移動(dòng)到“\0”;然后輸出結(jié)果。\\abd\0

  char* str這個(gè)是一個(gè)野指針,千萬別這樣使用,在類中除外。

小練習(xí)
#include <iostream>
using namespace std;

class Book{
private:
    
char* str;
public:
    Book(
char str[]);
    
void show();
}
;
Book::Book(
char str[]){
    
this->str=str;
}
;
void Book::show(){
    cout
<<this->str<<endl;
}


int main(){
    Book b(
"abc\0def");
    b
->show();
}
最后一行寫錯(cuò)了,應(yīng)當(dāng)是b.show();