以下是两个功能
StringBuffer* StringBuffer:: append(char c, StringBuffer& s){
StringBuffer* object= new StringBuffer(s);
refcount++;
cout<<refcount;
_length++;
object->_strbuf = (char*)realloc(_strbuf, _length);
object->_strbuf[_length-1]=c; //exception thrown here
object->_strbuf[_length]='\0';
//cout<<_strbuf;
}
和
StringBuffer:: StringBuffer(const StringBuffer& s){
int l= s.length();
_strbuf = (char*)malloc(sizeof(char)*l);
int loop=0;
while(loop<l){
_strbuf[loop]= s._strbuf[loop];
loop++;
}
_strbuf[loop]='\0';
_length= loop;
}
现在,refcount基本上是一个公共成员变量。当我在append()中打印出它的值时,它返回1.但是当我使用main()中的对象调用它时,它会显示垃圾值
StringBuffer* ob= new StringBuffer(*o);
cout<<ob->refcount;
StringBuffer* object=ob->append('l',*ob);
//StringBuffer* myobject=ob->append('f',*ob);
cout<<'\n';
object->display();
这是refcount
public:
int refcount=0;
对于ob,它返回0是正确的但对于对象它返回垃圾值。 我在这里尝试做的是,每当用户将某些内容附加到当前对象时,就会在append()中创建一个新对象,并对其进行更改。但问题是refcount不仅返回垃圾值,而且在尝试显示_strbuf时会引发异常