0

Ok so i want a std::vector of class objects. They will be made on demand.

std::vector<VertexBuffer> vBuffs;

somwehere::someFunction()
{
   VertexBuffer vB;
   thisNthat = 10;

   vB.thisNthat = thisNthat;
   ......
   vBuffs.push_back(vB);


}

Since vB goes out of scope, what happens to vBuffs[vB location] buffer?

What is the best way to do what i am trying to do.

vBuffs.push_back(VertexBuffer());

??

Then just assign values via the vector? (or constructor).

1 Answers1

1

"Since vB goes out of scope, what happens to vBuffs[vB location] buffer?"

Since the push_back()

vBuffs.push_back(vB);

puts a copy of vB to vBuffs it's no problem that it goes out of scope at the end of the function.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • If the class contains pointers is that a problem? I have written a copy constructor that copies pointers like this : *pVB = *copy.pVB; – Rear-Admiral Fluff Dec 29 '14 at 09:18
  • @Rear-AdmiralFluff It your class is broken, that's a problem. But `std::vector` is not generally broken (some might argue the `std::vector` specialization is, but that is another matter.) – juanchopanza Dec 29 '14 at 09:21
  • @Rear-AdmiralFluff That's a different case and your copy constructor is probably wrong. To maintain pointers in classes properly, you should obey [The rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – πάντα ῥεῖ Dec 29 '14 at 09:21
  • Broken usually is a problem, why is there something here which gives you that impression? Does *pVB = *copy.pVB present a problem? – Rear-Admiral Fluff Dec 29 '14 at 09:24
  • @Rear-AdmiralFluff Depends on how memory management is done for these members. But as I mentioned, thats a different question, not what you've been asking for in this one. – πάντα ῥεῖ Dec 29 '14 at 09:26