i want derived classa have method returns brand new object of classa. compiler warning returning reference local object.
some people suggested need implement move constructor. how that?
code 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 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 class "classa<t>" may not have 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 neither identical nor covariant 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]" @ line 26  test2.cpp(26): error: object of abstract class type "classa<int>" 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>" 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]" @ line 27  test2.cpp(13): error: object of abstract class type "classa<int>" 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]" @ line 27  compilation aborted test2.cpp (code 2)      
at least have error in following part:
classa<t>& operator[](int index){             classa<t> a; // <-- variable destroyed             return a; // , return reference         }   the return value reference temporal variable a destroyed after operator [] execution. 
i suggest fix error before all.
in addition, not have constructor initializes class.
putting constructor initializes, such as
classa<t>() {}   compiles gnu , clang
Comments
Post a Comment