// example: class constructor
#include <iostream>
#include <string>
class Test{
public:
char* getColor(){
return color;
}
private:
char color[5] = "Blau";
};
int main() {
Test s;
char *myChar = s.getColor();
std::cout << myChar;
return 0;
};
我真的不明白這實際上是如何返回「Blau」而不是隻是B或其他東西。分配字符數組到單個字符的奇怪行爲
我在做什麼是分配開始指針,如果一個字符數組到單個字符指針。
我真的很想理解爲什麼會發生這種情況。也許這是因爲std :: cout獲取該類型的所有值?因此,而不是「B」它說:「布勞」
'myChar'不是一個字符,它是一個字符指針。 – 2014-10-31 16:00:13