inheritance - Template syntax to accept an argument with the type of the derived object -



inheritance - Template syntax to accept an argument with the type of the derived object -

i have class template in there function takes argument of type of class inherits template. can't seem find right syntax bring about. example, if template:

// template promote simple c-style struct, t_c, // c++ class 't' constructors, destructors, // re-create , move assignment operators. template <typename t_c> class mytemplate { // in principle create templated re-create , move // constructors , assignment operators, they'd // implicitly deleted compiler unless explicitly defaulted // them: see // http://stackoverflow.com/questions/25246573/copy-assignment-operator-defined-in-template-being-deleted-by-compiler. // explicitly defaulting them works if template // contained in all-header library. however, on including // template-derived classes dll in visual studio 2013 // found that, when other methods // in class exported, defaulted // template methods not. there may workaround // this, in scheme of things thought easier // dispense templated operators , // create re-create , move assignment operators, // corresponding constructors, each derived type. // // can @ to the lowest degree simplify things little, , forcefulness // consistency upon our various class definitions, // insisting every relevant class defines // functions listed, can called // constructors , assignment operators create them bit // more manageable. // tidy pointers etc. , homecoming buffers safe state virtual void clear() = 0; protected: // build 't' t_c: virtual void construct_contents(const t_c &source) = 0; // deep re-create contents of t_c 't' virtual void copy_contents(const t_c &source) = 0; // move contents of 1 object another: utilize in both move constructor // , move assignment operator: virtual void move_contents(mytemplate<t_c> &&source) = 0; // sure wrong, can't figure out // right argument type should };

...and class

class myclass : public mystruct, public mytemplate<mystruct> { public: // default constructor myclass() {} // re-create constructor taking basic c struct myclass(const mystruct &source) { construct_contents(source); } // re-create constructor taking promoted c++ class myclass(const myclass &source) { construct_contents(source); } // re-create assignment operator taking basic c struct myclass & operator=(const mystruct &source) { copy_contents(source); homecoming *this; } // re-create assignment operator taking promoted c++ class myclass & operator=(const myclass &source) { copy_contents(source); homecoming *this; } // move constructor taking promoted c++ class myclass(myclass &&source) { move_contents(std::move(source)); } // move assignment operator taking promoted c++ class myclass & operator=(myclass &&source) { if (this != &source) { clear(); move_contents(std::move(source)); } homecoming *this; } // destructor ~myclass() { clear(); } // various getters , setters info fields of mystruct // .... virtual void clear() override { // stuff... } protected: virtual void construct_contents(const mystruct &source) override { // stuff... } virtual void copy_contents(const mystruct &source) override { // stuff... } virtual void move_contents(myclass &&source) override { // stuff... } };

...then works fine except compiler throws error saying (in visual studio 2013) 'member function declared "override" not override base of operations class member' when encounters definition of myclass::move_contents. can understand why unhappy: 'myclass' not only 'mytemplate<mystruct>', type jointly inherits both mytemplate<mystruct> and mystruct. need declare move_contents in template argument type same type of derived object, in case 'myclass'. however, can't think of way of specifying in template declaration.

my working solution take move_contents out of template altogether , define totally independent move_contents method each separate derived type. right, doing block off possibilty of pulling move constructor , assignment operator template - i'd love if can ever figure out way of getting visual studio 2013 export them properly.

the template parameter mystruct, method expects argument of type mytemplate<mystruct>, not myclass.

declare method :

virtual void move_contents(mytemplate<mystruct> &&source) override { // stuff... }

templates inheritance c++11 polymorphism

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -