类变量不保留C++值
C++ class variable does not retain values
我最近开始用C++编程(用Java做过)。我遇到类变量不保留其值的问题。
bt_builder.h
class BtreeBuilder{
BtreeNode *root; //will point to root of the tree
public:Status insertBuilderKey(KeyId);
.....
}
bt_builder.cpp
Status BtreeBuilder::insertBuilderKey(KeyId k){
....
BtreeIndex newroot ;
newroot.insertKey(Ld.getKey(0),0,left,right);
root = &newroot;
printnode(root);// prints correct values
....
}
bt_main.cpp
int main()
{
BtreeBuilder *btb = new BtreeBuilder();
btb->insertBuilderKey(1);//inside this method it has printed values corretly
btb->printroot();//now it is printing garbage values for root node it seems that the value which was set for root inside the method is no longer there
}
所以我的问题是为什么它不保留方法之外的root
值,即使它是一个类变量?
这个问题的解决方案是什么。
BtreeIndex newroot ;
在堆栈上创建了一个BtreeIndex
BtreeBuilder::insertBuilderKey
,但是一旦方法完成,保存该变量的堆栈帧就会被销毁,因此您的变量也会被销毁。
您需要在堆上创建BtreeIndex
:BtreeIndex* newroot = new BtreeIndex();
有关详细信息,请参阅 http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/。
你有类成员BtreeNode root;
,你从堆栈上的变量中为其赋值:BtreeIndex newroot
。当代码退出作用域(退出函数insertBuilderKey(KeyId k){...}
时,此变量将被销毁。如果需要分配新的根,请在堆BtreeIndex newroot = new BtreeIndex()
中创建它并继续。不要忘记删除之后需要的内容。
局部变量是自动存储持续时间的,并且在退出作用域时(例如从函数返回)时不复存在。
因此,保留其地址(在指针中)会导致悬空指针 - 指向不再存在的内容的指针。 取消引用该指针会导致未定义的行为。
这与Java完全不同,Java依赖于垃圾检测器检测何时不再引用对象并销毁它 - 只要存在对它的引用,就可以使用它。
当您尝试插入树时:
Status BtreeBuilder::insertBuilderKey(KeyId k){
....
BtreeIndex newroot ;
newroot.insertKey(Ld.getKey(0),0,left,right);
root = &newroot;
printnode(root);// prints correct values
....
}
。创建一个BtreeIndex newroot
并使成员root
指向它。打印root
工作正常,但是当函数返回时,newroot
被删除,root
指向涅槃。如果您正确使用它,C++会照顾您删除内容。但是,如果要控制对象的生命周期,则必须使用 new
和 delete
.例如
Status BtreeBuilder::insertBuilderKey(KeyId k){
....
root = new BtreeIndex();
root->insertKey(Ld.getKey(0),0,left,right);
printnode(root);// prints correct values
....
}
另一方面,如果BtreeBuilder
"拥有"BtreeIndex
,为什么不存储对象而不是指针? 即
BtreeIndex root;
Status BtreeBuilder::insertBuilderKey(KeyId k){
....
root.insertKey(Ld.getKey(0),0,left,right);
....
}
- 有根的二进制搜索树.保留与其父级的链接
- 为多个会话保留XPtr
- 保留对其他类的成员函数的引用
- 指针保留字符串
- 是否有内置方法可以强制转换为不同的基础类型,但保留常量限定符?
- 如何让 GCC/Clang 在保留标识符上出错
- 必须为 C++20 协程帧保留多少内存?
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 局部变量保留函数中的值
- 保留函数指针模板参数
- 变量超出范围后如何保留向量值?
- C++矢量复制构造函数和赋值运算符是否也复制保留空间?
- 当为可变性配置时,boost::heap::d_ary_heap 保留的额外 std::list 的目的是什么?
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 即使在使用 delete[] 后仍保留的元素
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 将成员函数保留为未定义
- C++:如何为多个重载函数保留通用代码路径?
- C++ 删除存在于另一个矢量中的矢量项,同时保留顺序