本文介绍了指针指向的指针不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编辑:我已经修正了 func2()
中的意外参数typo为 MyClass *
不是 MyClass
。
I have fixed the accidental argument typo in func2()
to be MyClass*
not MyClass
.
我有这样的类:
#include "MyClass.h"
class X{
public:
X();
MyClass* m;
func1();
func2(MyClass* m, int x);
private:
}
Source:
#include "X.h"
X::X{
m = null_ptr;
}
X::func1(){
//Pass the un-initialized data member m here...
func2(m, 6);
//When I get to this point m has not been assigned, even though it was passed as a pointer to func2()
}
X::func2(MyClass* old_obj, int x){
//Please forgive the memory management for a second....
MyClass* new_obj = new MyClass(x);
//This should initialise the data member m??????
old_obj = new_obj ;
}
但是它不工作 - 我在这里做一个基本的假设?我想这将工作....
However it doesn't work- am I making a fundamental miss-assumption here? I thought this would work....
推荐答案
要修改指针从函数参数,你需要修改原始指针不是复制。
To modify a pointer from function parameter, you need to modify original pointer not it's copy.
func2(MyClass*& m, int x);
// ^
这篇关于指针指向的指针不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!