boost::ptr_vector 和 boost::any 的问题
好吧,所以我有一个疑问,我想知道这是否可能:
我正在使用一个数据库,其中包含通用数据(字符串、整数、布尔值等)。 每当构造一个对象或修改对象的成员时,我都必须使用特定操作(SELECT 或 UPDATE)查询数据库。 首先,这不是一个与数据库相关的问题,我真正的问题是我有一个 ptr_vector ,它保存 boost::any 指向对象成员的指针。 在类似这样的代码中:
class Enemy{
private:
//some private data...
public:
auto_ptr<int> ID_Enemy;
auto_ptr<string> Enemy_Name;
//miscellaneous methods...
};
然后我将要修改的成员传递给另一个杂项类的函数,该函数以 boost::any* 作为参数:
misc_class.addValues((boost::any*)(ID_Enemy.get()));
misc_class.addValues((boost::any*)(Enemy_Name.get()));
同一个类接受 any*,并执行以下操作:
auto_ptr<boost::any> val2(val); //being val, the passed any*
Enemy_Values.push_back(val2);
Enemy_Values 是 ptr_vector 。 因此,当我访问这个以 Enemy_Values 作为成员的 Misc_class 时,我想更改内部 auto_ptr 指向的值:
misc_class.Enemy_Values[0] = (boost::any)(69);
在这里,我收到一个违规错误。 我尝试了很多事情,有人告诉我,我不应该使用 auto_ptr 容器或使用 boost::any 来回转换。 我这样做可能吗,还是有更好、更直观的方法?
提前致谢。
ok, so I got a doubt, I want to know if this is possible:
I'm using a database, with generic data (strings, ints, bools, etc...). Whenever an object is constructed or a member of an object is modified, I have to query the database with the specific action (SELECT or UPDATE).
First of all, this isn't a DB related question, my real problem is that I have a ptr_vector which holds boost::any's pointers to members of the object. In 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 I want to modify to a function of another miscellaneous class which takes as argument a boost::any*:
misc_class.addValues((boost::any*)(ID_Enemy.get()));
misc_class.addValues((boost::any*)(Enemy_Name.get()));
that same class accepts the any*, and does the following:
auto_ptr<boost::any> val2(val); //being val, the passed any*
Enemy_Values.push_back(val2);
Enemy_Values is a ptr_vector. So when I access this misc_class which has Enemy_Values as member, I want to change the value to which an auto_ptr inside is pointing:
misc_class.Enemy_Values[0] = (boost::any)(69);
And here, I get a violation error. I've tried many things, and someone told me that I shouldn't be using containers of auto_ptr or converting back and forth with boost::any. Is this that I am doing possible, or there is a better and more intuitive way?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(boost::any*)(ID_Enemy.get())
执行reinterpret_cast
因为您正在转换不相关的指针类型。 这意味着您会得到一个指向any
的无效指针,它指向真正的整数。相反,构造一个临时的 boost::any 对象并通过引用将其传递给 addValues:
您对
auto_ptr
的使用实际上是不正确的:auto_ptr
删除了 freestore 上的对象,但在这里我们而是与当地人打交道。addValues
只需要将any
对象的值推入向量:...而 Enemy_Values 应该只是一个 std::vector。
您可以使用 ptr_vector 和 freestore 分配的
boost::any
对象来完成此操作,但这会比必要的更复杂。(boost::any*)(ID_Enemy.get())
performs areinterpret_cast
since you are casting unrelated pointer types. This means you get an invalid pointer to anany
, pointing to what is really an integer.Instead, construct a temporary boost::any object and pass it by reference to addValues:
Your use of
auto_ptr
is in fact incorrect:auto_ptr
deletes objects on the freestore but here we're dealing with locals instead.addValues
merely needs to push the value of theany
object into the vector:... and Enemy_Values should just be a std::vector.
You could do this with a ptr_vector and freestore-allocated
boost::any
objects, but that would be more complicated than necessary.auto_ptr 有许多问题。 既然您已经在使用 boost,为什么不使用 boost::shared_ptr 呢?
auto_ptr has a number of problems. Since you are already using boost, why not use boost::shared_ptr instead?