C++ avoid casting in derived class code for twin container/contained object design -


i have twin base classes container class , contained object (in case, generic graph class made of linked nodes, guess question stand in cases want derive both container class , contained object class twin container/contained base classes). want use derived classes of both container , contained object. how avoid having downcast contained object derived contained object type when access container of derived container class?

// class represents graph template <typename wrappedobject> class basegraph  {   ... // constructor, virtual destructor, ... public:   std::map<wrappedobject*, basenode<wrappedobject> *> obj_to_node_map_;   bool addedge(wrappedobject *scr, wrappedobject *trg) {     basenode<wrappedobject > * srcnode = getorcreatenode(src);     basenode<wrappedobject > * dstnode = getorcreatenode(trg);     return addedge(srcnode, dstnode);   }    bool addedge(basenode<wrappedobject> * src, basenode<wrappedobject> * trg) {     if (src == trg) {       return false;     }      src->suc_.insert(trg);     trg->pre_.insert(src);     return true;   }    basenode<wrappedobject>* getorcreatenode(wrappedobject*obj){     basenode<wrappedobject> *node = getnode(obj);     if (node == nullptr) {       node = createnode(obj);       obj_to_node_map_[obj] = node;     }     return node;   }    basenode<wrappedobject>* createnode(wrappedobject* obj){     basenode<wrappedobject> *node = allocatenode(obj);     nodes_.push_back(node);     return node;   }   basenode<wrappedobject>* getnode(wrappedobject*obj) const {     auto = obj_to_node_map_.find(obj);     if (it == obj_to_node_map_.end()) {       return nullptr;     }     return it->second;   } private:   virtual basenode<object> * allocatenode(object *obj) = 0;  }; // class represents vertices of graph template <typename wrappedobject> class basenode {   friend class basegraph<wrappedobject>; public:   typedef std::set<basenode *> set;   basenode(wrappedobject*obj):obj_(obj) {   }   virtual ~basenode() {};    wrappedobject *obj_;   set suc_;   set pre_; };  // derived classes node , graph class realobject; class mynode : public basenode<realobject> { private:   std::string name_;   mynode(realobject *obj, std::string & name): basegraph<realobject>(obj), name_(name) {} ... }; class mygraph: public basegraph<realobject> {   // constructor, virtual destructor, ... private:   virtual basenode<realobject> * allocatenode(realobject *obj) {     return new mynode(obj, this);   }   std::map<std::string, mynode *> name_lu_map_; public:   mynode * getnode(std:string & name) {     return(name_lu_map_[name]);   }   // have have otherwise call getnode on mygraph instance   // cause compiler error getnode overloaded in derived class   mynode * getnode(realobject *obj) const {     return static_cast<mynode *>(basegraph<realobject>::getnode(obj));   }    void myderivedclassfunction(realobject *obj) {     // lines following when call method of base class     // returning base class pointer require casting, find     // ugly     mynode *node = static_cast<mynode *>(getorcreatenode(obj));     node->name_ = <something>;     ...   } }; 

is there design pattern allow avoid awkward downcast derived class whenever use methods of container base class return base class contained object pointers? thought of using template argument define derived contained object type be, not see how can describe in c++ fact typename template argument derives class. possibly, looking @ wrong angle. ideas?


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -