【问题标题】:Template Move Constructor模板移动构造函数
【发布时间】:2016-09-01 20:47:58
【问题描述】:

我希望派生的 ClassA 具有返回 ClassA 的全新对象的方法。我收到关于返回对本地对象的引用的编译器警告。

有人建议我需要实现一个移动构造函数。该怎么做?

不起作用的代码:

#include <iostream>
using namespace std;
template <typename T>
class AbstractClass {
    public:
        virtual AbstractClass<T>& operator[](int index) = 0;
} ;
template <typename T>
class ClassA : public AbstractClass<T> {
    public:
        ClassA<T>& operator[](int index){
            ClassA<T> A;
            return A;
        }
        ClassA(ClassA && c){
            //move constructor that doesn't work.
        }
} ;
template <typename T>
class ClassB : public ClassA<T> {
    public:
        ClassA<T>& operator[](int index){
            ClassA<T> A;
            return A;
        }
} ;
int main(void){
    ClassA<int> A;
    A[0][1][2];
}

错误信息(intel icc):

test2.cpp(15): error: copy constructor for class "ClassA<T>" may not have a parameter of type "ClassA<T>"
          ClassA(ClassA && c){
                 ^

另一个版本:

#include <iostream>
using namespace std;
template <typename T>
class AbstractClass {
    public:
        virtual AbstractClass<T> operator[](int index) = 0;
} ;
template <typename T>
class ClassA : public AbstractClass<T> {
    public:
        ClassA<T>() {}
        ClassA<T> operator[](int index){
            ClassA<T> A;
            return A;
        }
} ;
template <typename T>
class ClassB : public ClassA<T> {
    public:
        ClassA<T> operator[](int index){
            ClassA<T> A;
            return A;
        }
} ;
int main(void){
    ClassA<int> A;
    A[0][1][2];
}

错误(英特尔 icc):

test2.cpp(12): error: return type is neither identical to nor covariant with return type "AbstractClass<int>" of overridden virtual function "AbstractClass<T>::operator[] [with T=int]"
          ClassA<T> operator[](int index){
                    ^
          detected during instantiation of class "ClassA<T> [with T=int]" at line 26

test2.cpp(26): error: object of abstract class type "ClassA<int>" is not allowed:
            pure virtual function "AbstractClass<T>::operator[] [with T=int]" has no overrider
      ClassA<int> A;
                  ^

test2.cpp(12): error: function returning abstract class "ClassA<int>" is not allowed:
            pure virtual function "AbstractClass<T>::operator[] [with T=int]" has no overrider
          ClassA<T> operator[](int index){
                    ^
          detected during instantiation of "ClassA<T> ClassA<T>::operator[](int) [with T=int]" at line 27

test2.cpp(13): error: object of abstract class type "ClassA<int>" is not allowed:
            pure virtual function "AbstractClass<T>::operator[] [with T=int]" has no overrider
              ClassA<T> A;
                        ^
          detected during instantiation of "ClassA<T> ClassA<T>::operator[](int) [with T=int]" at line 27

compilation aborted for test2.cpp (code 2)

【问题讨论】:

  • 我在堆栈溢出和谷歌中查找了它。有些页面提到了移动构造函数,但很少有页面讨论模板移动构造函数。如果我知道它为什么不起作用,我不会问。
  • 添加您在问题中使用的编译器
  • 英特尔 icc。好吧。我刚刚找到“stackoverflow.com/questions/11028929/…”所以他们建议的“解决方案”实际上是行不通的。
  • operator[] 应该怎么做?您的意思是返回*this?请使用 C++11 标准编译。你需要在移动构造函数旁边声明ClassA() = default;
  • 混合多态性和模板时,您需要确定自己在做什么。即使作为示例,这段代码看起来也被过度设计了。

标签: c++ templates abstract-class


【解决方案1】:

至少你在以下部分有错误:

ClassA<T>& operator[](int index){
            ClassA<T> A; // <-- this variable will be destroyed
            return A; // and you return a reference to A
        }

返回值是对时间变量A的引用,在操作符[]执行后将被销毁。

我建议你先修复这个错误。

此外,您没有任何初始化类的构造函数。

放一些初始化的构造函数,比如

ClassA<T>() {}

使用gnuclang 编译

Un demo here

【讨论】:

  • 这是要解决的问题。如何解决?有人说移动构造函数。有人说返回一个指针。第一个解决方案不适用于模板。第二个似乎不好。
  • 这取决于你想做什么。如果想法是返回对象的副本,则首先删除 &amp; 限定符
  • 删除 & 将无法与抽象类一起使用...无法返回抽象类。
  • ClassA 不是抽象类。抽象类是AbstractClass。无论如何,我编辑了我的答案并放置了一个初始化状态的虚拟构造函数。之后,我实现了使用gnuclang进行编译
  • 你能添加完整的代码吗?我尝试将新代码放入问题中。
猜你喜欢
  • 2016-09-02
  • 2022-01-12
  • 2017-04-17
  • 1970-01-01
  • 2016-11-27
  • 2019-10-08
  • 2011-05-24
  • 2011-05-22
相关资源
最近更新 更多