为了更好地了解c++的内部机制,我决定编写自己的字符串类。然而,我卡在了 .toUpper()
和 .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;
while
循环的条件中的c++
。 - Sadique