【发布时间】:2018-02-02 11:32:21
【问题描述】:
我正在尝试返回一个指针并更改它所指向的内容,但我似乎无法让它工作。我是这样做的:
class someClass
{
public:
int *& foo();
private:
int * ptr = 5;
};
int *& someClass::foo()
{
return ptr;
}
int main()
{
int * ptrTwo = foo();
ptrTwo = NULL;
return 0;
}
我希望这会将 ptr 更改为 NULL。发生的事情是 ptr 不受影响,只有 ptrTwo 更改为 NULL。
【问题讨论】:
-
什么是
ptr,它在哪里声明或定义?说起来,className的定义在哪里? -
请显示minimal reproducible example。您实际上想要达到什么目的?
-
和
int x = 123; int& f() { return x; } /*...*/ int y = f(); y = 0;的情况一样,x不变。指针没有什么特别之处。 -
@Dante 不,你没有。
-
@Dante 您的问题中没有足够的信息,而且我不是唯一一个这么认为的人。您应该出示minimal reproducible example 并说明您期望发生的事情以及实际发生的事情。