title: About implementation of abstract/concrete graphs in C++
body: <p>I am writing a program that works with graphs. I am dealing with two types of graphs : "abstract graphs", which consist in abstract vertices with edges, and "planar graphs", in which the vertices have coordinates x,y in the plane (actually I am working with a complex affix z but it does not matter).</p>
  
  <p>I have elected to write an (abstract) Vertex class and a derived class Planar_Vertex as follows in my Vertex.h file - this is not precisely my code, I made it slightly simpler and "translated" it from French ;)</p>
  
  <pre><code>class Vertex
  {
  public:
      Vertex();
      int get_label();
      void set_label(int label);
      void add_neighbor(int label);
      bool is_neighbor(int label);
      // etc
  protected:
      int _label;
      std::vector&lt;int&gt; _list_neighbors;
  };
  
  
  class Planar_Vertex : public Vertex
  {
      complex&lt;double&gt; _affix;
  public:
      Planar_Vertex();
      Planar_Vertex(Vertex&amp; V, complex&lt;double&gt; affix);
      complex&lt;double&gt; get_affix();
      void set_affix(complex&lt;double&gt; affix);
  };
  </code></pre>
  
  <p>Here's my main question. I would like my Planar_Vertex(Vertex&amp; V, complex affix) constructor to have the following effects : 1. Output a Planar_Vertex whose label and list of neighbors are the same as V, and whose affix is given. So far, easy. 2. I would like V to be precisely the abstract Vertex underlying this new object. In other words, if in my main.cpp file I wrote, say,</p>
  
  <pre><code>Vertex V1;
  ...
  Planar_Vertex V2(V1,z)
  </code></pre>
  
  <p>then I would like that a use of set_label() on V2 will also affect V1 (for example). The way I see it is, in this constructor, I would like to say something like: make the address of V (in memory) the same as that of the Planar_Vertex constructed (and free the memory previously allocated to V). Apparently it is not possible to change the location of a variable in memory though, so I don't know what to do. I am relatively new to C++ and I'm getting lost reading about placement new, std::move, rvalues etc. Does anybody see how to do what I want?</p>
  
  <p><strong>[Edit : To summarize, I want to be able to build an object of the derived class on top of an object of the base class that has already been built.]</strong></p>
  
  <p>Okay, now since I told you guys that much about my implementation of graphs, I thought I'd tell you the rest so you can give me your opinion about it, I hope you don't mind. Obviously you don't have to read the following, if you know the answer to my first question that'd be cool already. So as I told you we're dealing with "abstract graphs", which will consist in abstract vertices, and planar graphs, which consist in planar vertices.</p>
  
  <p>Here's what my Graph.h file looks like:</p>
  
  <pre><code>class Graph
  {
  public:
      Graph();
      virtual ~Graph();
      virtual std::vector&lt;Vertex*&gt; get_list_vertices();
      void add_edge(int label1, int label2);
      virtual void add_vertex(Vertex&amp;);
      // etc
  };
  
  class Abstract_Graph : public Graph
  {
      std::vector&lt;Vertex*&gt; _list_vertices;
  public:
      Abstract_Graph();
      ~Abstract_Graph();
      std::vector&lt;Vertex*&gt; get_list_vertices();
      void add_vertex(Vertex&amp; V);
      // etc
  };
  
  class Planar_Graph : public Graph
  {
      std::vector&lt;Planar_Vertex*&gt; _list_planar_vertices;
  public:
      Planar_Graph();
      ~Planar_Graph();
      std::vector&lt;Vertex*&gt; get_list_vertices();
      std::vector&lt;Planar_Vertex*&gt; get_list_planar_vertices();
      void add_vertex(Planar_Vertex&amp; V);
      // etc
  };
  </code></pre>
  
  <p>My idea is that the base class Graph will never be instanciated, but I will be able to implement "abstract graph operations" as functions of this base class and they will work on both Abstract_Graph and Planar_Graph objects. This is made possible thanks to the purely virtual function get_list_vertices. Is this a reasonable way to do things? What would you have done?</p>
  
  <p>Thank you very much for for answers in advance.</p>