rxu
rxu

Reputation: 1407

Template Move Constructor

I want the derived ClassA to have a method that returns a brand new object of ClassA. I get compiler warning about returning reference to a local object.

Some people suggested I need to implement a move constructor. How to do that?

Code that doesn't work:

#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];
}

Error message (intel icc):

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

Another version:

#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];
}

Error (intel 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)

Upvotes: 0

Views: 752

Answers (1)

lrleon
lrleon

Reputation: 2648

At least you have an error in the following part:

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

The return value is a reference to the temporal variable A which will be destroyed after operator [] execution.

I suggest you to fix this error before all.

In addition, you do not have any constructor that initializes the class.

Putting some constructor that initializes, some such as

ClassA<T>() {}

compiles with gnu and clang

Un demo here

Upvotes: 1

Related Questions