标签:strcpy new using 必须 n+1 name har style include
1 #include <iostream> 2 using namespace std; 3 4 class MyString{ 5 private: 6 char * p; 7 int len; 8 public: 9 MyString(const char *s){ 10 len = strlen(s); 11 p = new char[len+1]; 12 strcpy(p,s); 13 } 14 /* 15 MyString(char *s){ 16 len = strlen(s); 17 p = new char[len+1]; 18 strcpy(p,s); 19 } 20 */ 21 }; 22 int main(){ 23 MyString s1("aaa"); 24 return 0; 25 }
为什么构造函数里必须要加const,猜测是因为"aaa"是一个常量,待解决
标签:strcpy new using 必须 n+1 name har style include
原文地址:https://www.cnblogs.com/zzsama/p/10767798.html