Hello!
I'm learning my way through std vectores and c++ and can't understand one thing: how do I write code that uses the automatic way of handling memory (constructores & destructores)?
What I want to do (inside a function that returns a pointer to the created object):
- Create one Item with the atribute name set to "ball"
- Put it inside a global vector
- return the pointer to the created Item.
This sample code is the best idea of how to use I came up with. I'm pretty sure I'm missing something.Code:Item* makeItem(){ std::vector<Item> bag; bag.push_back(*(new Item())); bag.back().name = "ball"; return &(bag.back()); }
Is this really the way handle the references vs objects?



LinkBack URL
About LinkBacks



