为了进一步了解 c++ 的内部工作原理,我决定编写自己的字符串类。但是我被困在.toUpper()
and.toLower()
功能上。这是我的代码。
Text& toUpper(){
char* c = this->str;
while(*c != 0, c++){
if((*c >= 'a') && (*c <= 'z')){
*c = *c - 32;
std::cout << *c << std::endl;
}
}
return *this;
}
我已经隔离了导致分段错误的线路,*c = *c - 32
但我不明白为什么这会导致问题。我试过(char)(*c - 32)
了,但没有用。这也不是一个边界问题,因为没有任何输出。有任何想法吗?
更新:我的构造函数
Text(char* str){
this->str = str;
this->updateLength(); // glorified strlen
}
我的指针定义
private:
char* str;
int len;