c++ - 有没有办法在 C++ 中通过引用传递右值?

标签 c++ stack binary-search-tree rvalue lvalue

我正在尝试为小型数据库分配创建“回滚”功能。 我有一堆二叉搜索树,用于存储名为的数据库的备份:

GenStack<GenBST<Student>> masterStudentStack;

堆栈和 BST 都是我自己的实现(根据我的作业说明)。

我可以毫无问题地将 BST 的拷贝插入堆栈,

masterStudentStack.push(*masterStudent);

但是,当我尝试检索此 BST 并将其返回到我的主 BST 指针时 使用:

void rollBack() {
    masterStudent = new GenBST<Student>(masterStudentStack.pop());
}

我收到错误。

Menu.cpp:419:63: error: invalid initialization of non-const reference of 
type ‘GenBST<Student>&’ from an rvalue of type ‘GenBST<Student>’
 masterStudent = new GenBST<Student>(masterStudentStack.pop());
                                     ~~~~~~~~~~~~~~~~~~~~~~^~
In file included from Menu.h:7:0,
             from Menu.cpp:1:
GenBST.h:49:1: note:   initializing argument 1 of 
‘GenBST<T>::GenBST(GenBST<T>&) [with T = Student]’
 GenBST<T>::GenBST(GenBST<T>& other) {
 ^~~~~~~~~

当左值通过引用传入时,BST 的复制构造函数起作用(这是构造函数的声明)

GenBST(GenBST<T>& other);

但我不知道如何以复制构造函数接受的方式从堆栈中弹出某些内容。所以,我的问题是:我可以使用右值“stack.pop()”创建一个新的 BST 吗?

谢谢, 马修

编辑:

将“const”添加到我的 BST 复制构造函数后,出现此错误

In file included from Menu.h:7:0,
             from Menu.cpp:1:
GenBST.h: In instantiation of ‘GenBST<T>::GenBST(const GenBST<T>&) [with T = 
Student]’:
Menu.cpp:419:65:   required from here
GenBST.h:50:22: error: passing ‘const GenBST<Student>’ as ‘this’ argument 
discards qualifiers [-fpermissive]
   if(other.getRoot() == NULL) {

GenBST.h:77:17: note:   in call to ‘GenTreeNode<T>* GenBST<T>::getRoot() 
[with T = Student]’
 GenTreeNode<T>* GenBST<T>::getRoot()
                 ^~~~~~~~~
GenBST.h:54:32: error: binding ‘GenTreeNode<Student>* const’ to reference of 
type ‘GenTreeNode<Student>*&’ discards qualifiers
     copyTree(this->root, other.root);
                          ~~~~~~^~~~
GenBST.h:108:6: note:   initializing argument 2 of ‘void 
GenBST<T>::copyTree(GenTreeNode<T>*&, GenTreeNode<T>*&) [with T = Student]’
 void GenBST<T>::copyTree(GenTreeNode<T> *& thisNode, GenTreeNode<T> *& 
otherNode) {

这是我的构造函数及其调用的方法:

template <class T>
GenBST<T>::GenBST(const GenBST<T>& other) {
  if(other.getRoot() == NULL) {
    root = NULL;
  }
  else {
    copyTree(this->root, other.root);
  }
}

template <class T>
void GenBST<T>::copyTree(GenTreeNode<T> *& thisNode, GenTreeNode<T> *& 
otherNode) {
  if(otherNode == NULL) {
    thisNode = NULL;
  }
  else {
    thisNode = new GenTreeNode<T>(otherNode->key);
    copyTree(thisNode->left, otherNode->left);
    copyTree(thisNode->right, otherNode->right);
  }
}

有什么想法吗?

编辑2:

非常感谢大家的帮助。我向 getRoot() 和 copyTree() 方法添加了 const,现在只剩下一个错误。

GenBST.h: In instantiation of ‘GenBST<T>::GenBST(const GenBST<T>&) [with T = 
Student]’:
Menu.cpp:419:65:   required from here
GenBST.h:54:32: error: binding ‘GenTreeNode<Student>* const’ to reference of 
type ‘GenTreeNode<Student>*&’ discards qualifiers
     copyTree(this->root, other.root);
                          ~~~~~~^~~~
GenBST.h:108:6: note:   initializing argument 2 of ‘void 
GenBST<T>::copyTree(GenTreeNode<T>*&, GenTreeNode<T>*&) const [with T = 
Student]’
 void GenBST<T>::copyTree(GenTreeNode<T> *& thisNode, GenTreeNode<T> *& 
otherNode) const {   

最佳答案

复制构造函数的规范形式采用对要复制的 const 对象的引用。从概念上讲,制作某物的拷贝通常意味着原始对象保持不变。几乎不需要修改您正在复制的对象。右值可以绑定(bind)到 const 引用,但不能绑定(bind)到非 const 引用。除非制作 GenBST 的拷贝确实需要修改您正在复制的对象(我假设并真诚地希望它不会),否则您只需将复制构造函数的签名更改为

GenBST(const GenBST& other);

关于c++ - 有没有办法在 C++ 中通过引用传递右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53474381/

相关文章:

c - 带有 char** val 的结构

c++ - 这段扩展整数参数包的代码可以只用1个函数来写吗?

c++回文程序使用数组

c++ - LoadLibrary调用DLL时查找依赖的DLL

c++ - 在堆栈 C++ 的链表实现中跟踪堆栈大小

java - 使用 Stacks Java 中缀到 Postfix

c++ - Qsort 不能正确排序字符串数组

c++ - 二叉搜索树;自组织搜索/旋转 C++

c - 在二叉搜索树中通过引用重定向指针

java - 无法使用 TreeSet 的 contains() 方法