Quantcast
Channel: openFrameworks - Latest posts
Viewing all articles
Browse latest Browse all 40524

Vector init and for loop in C++11

$
0
0

the trick is that you need to get a reference to the object in the vector, if you do:

vector<particle> particles;

particles.resize(20); 

for ( auto p : particles) {
   p.setup(posX, posY, width, heiht);
}

you are making a copy and calling setup on that copy, if you want to modify the original in the vector the correct syntax is:

vector<particle> particles;

particles.resize(20); 

for ( auto & p : particles) {
   p.setup(posX, posY, width, heiht);
}

note the & between auto and p that makes p a reference to the object in the vector instead of a copy.


Viewing all articles
Browse latest Browse all 40524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>