C++ first-class object概念

C++ standard library P132看到一个新的概念first-class object.但是什么是first-class object?

下面是来自维基百科的解释:

A first-class object is one that can be dynamically created, destroyed or passed as an argument.So, for example, all objects in C++ are also first-class objects. They can be created or destroyed through use of constructor or destructor methods, respectively, and be passed as arguments to functions. However, functions and methods in C++ are not first-class, as they cannot be created at runtime.In functional languages, functions are first-class objects, as they can be passed around as arguments to other functions and can be created dynamically at run-time.In C++, classes are not first class -- though in some languages (LISP's CLOS, for example) they are (as classes are objects).
first-class object:(programming, languages) An entity [that can be constructed at run-time,] passed as a parameter, returned from a subroutine, or assigned into a variable.//first-class object是可以在运行时构造,可以作为参数进行传递,可以从子程序返回,或者赋值给变量。一个就是一种实体。当出现如下情况时,一个对象就成为 FCO:可以保存在变量和数据结构中;可以作为参数传递给子过程;可以作为子过程的运算结果返回;可以在运行时被构建;具有内在的一致性(独立于任何给定的名称)。
这里的“对象”不是严格意义上的,不一定是面向对象编程中的“对象”。最简单的标量数据类型,如整型和浮点型,几乎永远是first-class
(programming, languages) An entity that can pass a value as a parameter, can be returned from a subroutine, and can be assigned into a variable. (This is the definition according to Raphael Finkel, who uses the terms of second- and third-class objects.)

相关概念:
second-class object:(programming, languages)An entity of which the value can be passed as a parameter, but that can neither be returned from a function, nor can it be assigned to a variable.

third-class object: (programming, languages) An entity of which the value can neither be passed as a parameter, nor be returned from a function, nor assigned to a variable.

总结:

Manipulation First Second Third
Pass value as a parameter yes yes no
Return value from a procedure yes no no
Assign value into a variable yes no no
看了这么多回到第132页
std::vector<Myclass&> coll;                          //error
std::vector<std::reference_wrapper<Myclass>> coll;
我们知道vector所能存储的数据类型必须满足:1 元素类型支持赋值;2 我们必须能够拷贝元素类型的对象。但是引用类型并不支持赋值操作。所以第一个语句是错误的。

你可能感兴趣的:(C++ first-class object概念)