title: pass the reference of this class inside it's constructor to a vector of pointers C++
body: <p>Okay so i have a pointer vector of type 'Entity', and what i want to do is, when a new Entity type class gets constructed it gets pushed back to the vector of Entities, in C# this would be possible by passing 'this' as a parameter, in C++ it gives no error, but when i test it, the vector doesn't point to the newly contructed Entity !</p>
  
  <p>Here is some code:</p>
  
  <p>'Public.h' and 'Public.cpp' handle the public variables and functions, here is the pointer vector and pointer variable</p>
  
  <pre><code>vector &lt;Entity*&gt; AllEntities;
  Entity* lastEntity;
  </code></pre>
  
  <p>And here is the constructor of the class 'Entity'</p>
  
  <p>'Entity.cpp':</p>
  
  <pre><code>#include "Public.h"
  #include "Entity.h"
  
  // constructor
      Entity::Entity(string name, string tag)
      {
          ID = GetCounter();
          this-&gt;Name = UniqueName(name);
          this-&gt;Tag = UniqueTag(tag);
          AllEntities.push_back(this); // it doesn't give any errors
          lastEntity = this; // because i thought it was a problem with the vector i tried the same with a variable, but it doesn't work too
      }
  
  // Function that prints the name, tag, and id.
  
  void Entity::PrintAll(){
      cout &lt;&lt; "NAME: \"" &lt;&lt; Entity::Name &lt;&lt; "\" TAG: \"" &lt;&lt; Entity::Tag &lt;&lt; "\" ID: \"" &lt;&lt; Entity::ID &lt;&lt; "\"" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>// other code</p>
  
  <p>'Entity.h' doesn't do much, it only handles the declaration(or definition, not sure how its called) of variables and functions like Name, Tag, and ID !</p>
  
  <p>and here is my 'Main.cpp':</p>
  
  <pre><code>#include "SFML\Graphics.hpp"
  #include "SFML\System.hpp"
  #include "Public.h"
  #include "DisplayText.h"
  #include "SaySomething.h"
  // note that i'm including 'Entity.h' through 'Public.h' with Include Guards so i won't get any linker errors !
  
  int main()
  {
  // sfml stuff !
      sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
  
      Entity ent1 = Entity("ent1");
  
      ent1.PrintAll();
  
      AllEntities.at(0)-&gt;PrintAll();
  
      lastEntity-&gt;PrintAll();
  </code></pre>
  
  <p>// other code</p>
  
  <blockquote>
    <p>Entity ent1 = Entity("ent1");</p>
  </blockquote>
  
  <p>here i create a new entity, with the name "ent1", and it automatically gives it the tag "Entity0" and the ID "0" because its the first entity !</p>
  
  <blockquote>
    <p>ent1.PrintAll();</p>
  </blockquote>
  
  <p>This works correctly, it prints out the name("ent1"), tag and ID of this Entity !</p>
  
  <blockquote>
    <p>AllEntities.at(0)->PrintAll();</p>
  </blockquote>
  
  <p>This basically calls the function 'PrintAll' from the first Entity(which in this case is 'ent1'), and its supposed to print the same text as this: 'ent1.PrintAll()' but it does not, it prints:
      (Name: "" with Tag: "" and ID: ""), i thought i was not using the vector correctly but as shown below it doesn't work with pointers too:</p>
  
  <blockquote>
    <p>lastEntity->PrintAll();</p>
  </blockquote>
  
  <p>This doesn't work !</p>
  
  <p>I'm not sure what the problem is, and how to fix it, firstly i thought maybe something about the variables: 'Name' and 'Tag' and 'ID' doesn't work, but the problem is that the vector doesn't point to the variable, i've tried with lots of different functions, not only with printing name, but it doesn't 'track' the Entity that was created !</p>