【发布时间】:2020-05-10 11:36:14
【问题描述】:
我制作了一个简单的测试程序,因为我无法弄清楚为什么指针访问对象的整数值,而当在另一个范围内创建对象时它无法显示字符串变量。当我删除这些括号时,指针通常会返回字符串的变量,而使用括号时,这个字符串内什么都没有。
#include <iostream>
#include <stdlib.h>
using namespace std;
int test() {
cout << "NO ELO MORDECZKI" << endl;
return 1;
}
class TEST {
public:
int i;
int j;
string a;
TEST(int i, int j, string a) { this->i = i; this->j = j; this->a=a; }
void operator +(TEST b) {
this->i = this->i - b.i;
if (i < 0) {
cout << b.i << endl;
b.i -= - (test()*100);
cout << b.i << endl;
}
}
};
int main() {
TEST* l1;
TEST* l2;
{
TEST a{ 1,2, "asd" }, b{ rand() % 20 + 10,1, "asdf" };
l1 = &a;
l2 = &b;
}
*l1 + *l2;
cout << "->" << l1->i << "<-" << endl;
}
【问题讨论】:
标签: c++ string pointers scope undefined-behavior