c++ - Create multiple instances of class & store them in vector<class> -


what i'm trying create multiple instances of class point , access class variables each instance.

class point { public:     float x, y, z; }; 

in following main() program, i'm creating single instance of type point, called point_ , pushing same instance multiple times in vector.

int main()  {      std::vector < point > vect;     point point_;      int num_instances; // user defined      (int = 0; < num_instances; i++)     {         vect.push_back(point_); // (^^^)     } } 

(^^^) here, problem vector of type point adding 1 single instance (called point_) manually created. what want based on num_instances input user (say 5), same number (i.e. 5) of instances of type point should created , stored in vector (called vect).

in following main() program, i'm creating single instance of type point, called point_ , pushing same instance multiple times in vector.

yes creating single instance of type point called point_. no not pushing same instance multiple times in vector.

if pushing same instance vector multiple times, mean instance in multiple places @ once. cannot happen. i'll use analogy of physical paper document. if wanted put document pile of documents multiple times, have make copies of it. same goes point object.

when call push_back on vector, allocates memory point , copies given point object memory. since allocate separate chunks of memory each item in vector, there no way can same object.

edit: here's proof: http://ideone.com/f5xx4u


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -