Problems with boost :: ptr_vector and boost :: any
ok so I got a doubt, I want to know if this is possible:
I am using a database with shared data (strings, ints, bools, etc.). Whenever an object is created or a member of the object is modified, I have to query the database with a specific action (SELECT or UPDATE). First of all, this is not a DB related question, my real problem is that I have a ptr_vector that contains boost :: any pointers to the members of the object. In the code, something like this:
class Enemy{
private:
//some private data...
public:
auto_ptr<int> ID_Enemy;
auto_ptr<string> Enemy_Name;
//miscellaneous methods...
};
then I pass the members that I want to change to a function of another other class that takes boost :: any * as an argument:
misc_class.addValues((boost::any*)(ID_Enemy.get()));
misc_class.addValues((boost::any*)(Enemy_Name.get()));
the same class takes any * and does the following:
auto_ptr<boost::any> val2(val); //being val, the passed any*
Enemy_Values.push_back(val2);
Enemy_Values is ptr_vector. So when I access this misc_class which has Enemy_Values as a member, I want to change the value that auto_ptr points to internally:
misc_class.Enemy_Values[0] = (boost::any)(69);
And here I am getting a violation error. I've tried a lot of things and someone told me that I shouldn't use auto_ptr containers or convert back and forth with boost :: any. Is this what I am making possible, or is there a better and more intuitive way?
Thanks in advance.
(boost::any*)(ID_Enemy.get())
performs reinterpret_cast
as you are producing unbound pointer types. This means that you are getting an invalid pointer to any
, indicating what is indeed an integer. Instead, create a temporary boost :: any object and pass it by addValues:
misc_class.addValues(boost::any(ID_Enemy.get());
Your usage auto_ptr
is actually wrong: auto_ptr
deletes objects on the freestore, but here we are dealing with locals. addValues
just need to push the value of the object any
into a vector:
Enemy_Values.push_back(val);
... and Enemy_Values should only be std :: vector.
You can do this with dedicated objects boost::any
with ptr_vector and freestore, but it will be more difficult than necessary.
auto_ptr has a number of issues . Since you are already using boost, why not use boost :: shared_ptr instead?
a source to share