c ++的智能指针 [英] smart pointers for c++

查看:66
本文介绍了c ++的智能指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用于c ++的好的智能指针?我希望它们用于指针向量....

what''s a good smart pointer to use for c++?? I want them for use in a vector of pointers....

推荐答案

这个问题真的没有意义。


你用术语智能指针是什么意思?


一个指向什么的向量的向量?


你应该怎么做尽量避免使用void指针向量(void *)。它们听起来可能是一个好主意,但C ++的强类型对于确保一个危险的书面工作程序很有用。使用void *只是围绕这个。


那么你的向量中的指针指向什么?它是一个特定的类型,比如int,在这种情况下你可以使用int *,或者一个类或结构,在这种情况下,你可以再次使用指向该类或结构的指针。


或者您需要指向各种不同的课程。在这种情况下,您应该看到类之间是否存在某种类型的链接,允许您在层次结构中声明结构,并使用指向层次结构中基类的指针声明向量。


如果没有这个可以做到这一点,那么你可能必须声明一个联合,但是因为你需要能够告诉联盟中的哪个项目是正在使用的那个项目,那么你可能需要做一些像声明一样的事情。枚举标识正在使用的联合成员和包含枚举和联合条目的结构以及带有指向此结构的指针的向量。
This question doesn''t really make sense.

What do you mean by the term "smart pointer"?

A vector of pointers to what?

What you should try to do is avoid using a vector of void pointers (void *). They may sound like a good idea but the strong typing of C++ is useful in ensuring a rigerously written working program. Using void * just gets round this.

So what are the pointers in your vector pointing at? Is it a specific type, like int, in which case you can use int *, or a class or structure in which case you can, again, just use a pointer to that class or structure.

Or do you need to point to a variety of different classes. In this case you should see if there is some sort of link between the classes that allows you to declare the structures in a hierarchy and declare you vector with pointers to the base class in the hierarchy.

If none of that will do the trick then you may have to declare a union, however since you will need to be able to tell which item in the union is the one in use then you will probably have to do something like declare an enum identifying the union member in use and a structure containing an enum and a union entry and the vector with pointers to this structure.


是的,向量中的指针指向类对象。类中有不同的构造函数来初始化具有不同指针数的类对象。但指出的所有物体基本相同,是的。


我不能删除任何对象,我认为问题在于我用它们来指导它们。指向对象的指针在向量中乱窜了很多,我不确定向量是处理指针的最佳位置......
yeah, the pointers in the vector are pointing to class objects. there are different constructors in the class to initialize class objects with a different number of pointers. but all objects being pointed to are essentially the same, yeah.

I can''t ever delete any of the objects, and I think the problem lies with the pointers I use to new them. the pointers to the objects are shuffled around in the vectors quite a bit, and I''m not sure that the vector is the best at handling pointers...


如果它始终是同一类为什么使用指针只是有一个像这样的类的向量

If it is always the same class why use pointers at all just have a vector of classes like so

展开 | 选择 | Wrap < span class =codeDivider> | 行号


这篇关于c ++的智能指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆