指针到指针分配不起作用
Pointer to pointer assignment isnt working
编辑:我已经修复了func2()
中意外的参数错别字,以MyClass*
而不是MyClass
。
我有这样的课程:
#include "MyClass.h"
class X{
public:
X();
MyClass* m;
func1();
func2(MyClass* m, int x);
private:
}
源:
#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 ;
}
但是它不起作用 - 我在这里做了一个基本的错误假设吗?我以为这会起作用。
要从函数参数修改指针,您需要修改原始指针而不是副本。
func2(MyClass*& m, int x);
// ^
相关文章:
- 从向量到空指针的 memcpy(反之亦然)不起作用
- 指向类中函数方法的指针不起作用
- 在 c++ 中返回矢量的指针不起作用
- 班上的Arduino步进电动机指针不起作用
- 功能指针不起作用(C )
- 投射时共享指针不起作用?
- 指向结构的指针不起作用
- 指针不起作用,返回地址不是值
- 尝试调用成员函数指针不起作用
- 矩阵乘法中的指针不起作用
- 指针不起作用
- 为什么交换2d数组指针不起作用
- 指向函数的指针不起作用
- 为什么使用本地指针不起作用
- C++ 我的字符串类:指针不起作用
- 递增指针不起作用
- 放置指向shared_ptr的多重映射的指针不起作用
- 使用正则表达式和 sed 命令替换C++代码中的' *'指针不起作用
- 对象指针不起作用
- 数据结构 - C++指针不起作用