为了练习,我正在编写我的字符串类,我需要初始化一个 int strLen类型的动态变量,我遇到了一个问题,特别是增量操作难以理解:
//компилятор clang
String::String(char* str){
int* strLen = new int(0);
std::cout << "_______\ncell:\t " << strLen << "\nvariable = \t" << *strLen << "\n_______\n";
*strLen++;
std::cout << "_______\ncell:\t " << strLen << "\nvariable = \t" << *strLen << "\n_______\n";
}
输出结果发现增加的不是值,而是地址:
_______
cell: 0x19a7dea1960
variable = 0
_______
_______
cell: 0x19a7dea1964
variable = -1414812757
_______
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
老实说,我不明白为什么会发生这种情况,因为正如我所想,带有指针符号的变量应该改变值,而不是地址。请告诉我。
预先非常感谢您!
操作优先级...