将字符c重复n次得到字符串(case 3的情况), 为什么输出时会多输出未知的3个,空间应该没有多开啊!哪位大佬帮忙看一下,蟹蟹
#include <iostream>#include <cstring>
using namespace std;
class MyString
{
private:
char *str;
static int count;
public:
static int GetCount()
{
return count;
}
MyString(unsigned n,char c)
{
str = new char(n);
while(n)
{
str[n-1] = c;
n--;
}
++count;
}
MyString(char *p = NULL)
{
str = p;
++count;
}
void ShowStr()
{
cout << str;
}
};
int MyString::count=0;
void fun1(int n)
{
MyString strArr[n];
cout<<strArr[0].GetCount()<<endl;
}
void fun2(int n)
{
cout<<MyString::GetCount()<<endl;
}
void fun3(MyString &s1, MyString s2)
{
MyString s3;
s3=s1=s2;
cout<<MyString::GetCount()<<endl;
s3.ShowStr();
cout<<endl;
}
int main( )
{
MyString s1;
MyString s2;
int n;
char charArr[20];
int op;
while (cin >> op)
{
switch (op)
{
case 1:
{
cin>>n;
fun1(n);
break;
}
case 2:
{
cin>>n;
fun2(n);
break;
}
case 3:
{
int m;
char ch;
cin>>m>>ch;
s2=MyString(m,ch);
fun3(s1,s2);
s1.ShowStr();
cout<<endl;
break;
}
case 4:
{
cin >> charArr;
s1=MyString(charArr);
cout<<MyString::GetCount()<<endl;
s1.ShowStr();
cout<<endl;
break;
}
}
}
return 0;
}