简体   繁体   English

如何将对象添加到矢量 <const Obj&> ?

[英]How do I add an object to a vector<const Obj&>?

I am not sure why this doesn't compile: 我不知道为什么这不编译:

std::vector< const Obj& > myVector;

void foo(const Obj& obj) 
{
    myVector.push_back( obj );  
}  

Sorry, a bit of additional info on what I'm trying to achieve: I can't change the signature of foo without breaking an interface, but I just want to hang onto the Objects that get passed through foo. 对不起,关于我想要实现的内容的一些额外信息:我不能在不破坏接口的情况下更改foo的签名,但我只想挂在通过foo传递的对象上。 I do want to store pointers to them, but I'm confused about the syntax of how to do that. 我确实想存储它们的指针,但我对如何做到这一点的语法感到困惑。

You can't have an vector of references. 你不能有引用的向量。 as the things in a vector must be copyable and assignable, and references are neither of these. 因为向量中的东西必须是可复制和可分配的,并且引用都不是这些。 You probably want a vector of pointers: 你可能想要一个指针向量:

std::vector< const Obj * > myVector;

void foo( const Obj & obj )
{
   myVector.push_back( & obj );
}

I'm not sure if you can put references into a container like this. 我不确定你是否可以将引用放入这样的容器中。 Use 采用

 std::vector<const Obj *> myVector

instead as that's semantically equivalent anyway. 相反,因为它在语义上是等价的。

You cannot use references as the types in a vector. 您不能将引用用作向量中的类型。 You can use raw pointers, smart pointers (boost/std::tr1 shared_ptr) or other constructs like boost::ref and boost::cref that provide wrappers around plain references to adapt to containers. 您可以使用原始指针,智能指针(boost / std :: tr1 shared_ptr)或其他构造,如boost::refboost::cref ,它们提供围绕普通引用的包装器以适应容器。

Vectors only work with value types; 向量仅适用于值类型; you cannot have a vector of references. 你不能有引用的向量。

Part of the reason is that references must be bound to an existing variable as they are created, and they can never be re-bound. 部分原因是引用必须在创建时绑定到现有变量,并且永远不能重新绑定它们。 For a vector to be able to hold 10 elements at their "default" value is impossible with a reference type: 对于能够以“默认”值保存10个元素的向量,使用引用类型是不可能的:

std::vector<const T& obj>(10); // cannot work

Similarly, you cannot re-assign a value to a reference (unless your intention was to modify the value of the original variable) so suddenly myVec[0] = 7 doesn't work either. 同样,你不能为引用重新赋值(除非你打算修改原始变量的值)所以突然myVec[0] = 7也不起作用。

Alternatively you can use boost::ref to store references, but it is much like storing pointers. 或者你可以使用boost::ref来存储引用,但它很像存储指针。

 std::vector< boost::ref< Obj > > myVector;

As per your edits, if you want to store pointers, you can just write something like: 根据您的编辑,如果您想存储指针,您可以编写如下内容:

std::vector < const Obj* > myVector;
void foo(const Obj& obj)
{
   myVector.push_back(&obj);
}

If you use boost::ref or boost::cref: 如果你使用boost :: ref或boost :: cref:

std::vector < boost::cref < Obj > > myVector;
void foo(const Obj& obj)
{
   myVector.push_back(boost::cref< Obj >(obj) );
}

(probably you can ommit the last boost::cref in the body too, but I don't have now a compiler at hand). (可能你也可以省略身体中的最后一个boost :: cref,但我现在没有编译器)。

The C++ Standard in chapater 23.1 Container requirements specifies: 章节23.1中的C ++标准容器要求指定:

3 The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types. 3存储在这些组件中的对象类型必须满足CopyConstructible类型(20.1.3)的要求,以及可分配类型的附加要求。

In other words, a type T is usable with standard C++ containers as value type as long as it defines copy constructor. 换句话说,类型T可以与标准C ++容器一起用作值类型,只要它定义了复制构造函数即可。 The CopyConstructible concept is explain by the standard in the cited chapater but Boost manual explains clearly and shortly what means a type is CopyConstructible CopyConstructible概念由引用的章节中的标准解释,但Boost手册清楚地解释了什么意味着类型是CopyConstructible

Reference does not fulfil this requirement. 参考不符合此要求。

You cannot store references in an STL container because references are not copy constructable. 您不能将引用存储在STL容器中,因为引用不是可复制构造的。 TR1 contains a reference wrapper if you really need to "simulate" storing references. 如果你真的需要“模拟”存储引用,TR1包含一个引用包装器。 Storing smart pointers (eg tr1::shared_ptr) or object instances is usually better. 存储智能指针(例如tr1 :: shared_ptr)或对象实例通常更好。

Aside from problems with references being the target type for a container, you can't have const objects as the target type for a container because the type of objects that a container is instantiated with must be "Assignable" as well as "CopyConstructable" (23.1 Container requirements). 除了引用是容器的目标类型的问题之外,您不能将const对象作为容器的目标类型,因为容器实例化的对象类型必须是“可分配”以及“CopyConstructable”( 23.1集装箱要求)。 Const objects aren't assignable. Const对象不可分配。

To be assignable, after a 't = u operation, t must be equivalent to u where t is of type T - the type the container was instantiated with. 要进行赋值,在't = u操作之后, t必须等于u ,其中tT类型 - 容器实例化的类型。

However, as mentioned in other answers, you can put pointers to const objects in containers. 但是,如其他答案中所述,您可以在容器中添加指向const对象的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM