`
xuning2516
  • 浏览: 8030 次
  • 性别: Icon_minigender_1
  • 来自: 江西
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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++实现队列存储Object源代码

    C++允许我们定义自己的数据类型,通过创建类(Class)来实现。在这个案例中,我们将创建一个名为`Customer`的类,它可能代表某种业务场景下的客户信息。队列将用于管理这些`Customer`对象,确保它们按照入队的顺序...

    Biblio.Distribution.C++.For.Artists.The.Art.Philosophy.and.Science.of.Object-Oriented.Programming.2003.chm

    C++ For Artists: The Art, Philosophy, and Science of Object-Oriented Programming by Rick Miller ISBN:1932504028 Biblio Distribution ? 2003 (590 pages) Intended as both a classroom and reference ...

    Object- Oriented Programming with Ansi-C

    The first six chapters develop the foundations of object-oriented programming with ANSI-C. We start with a careful information hiding technique for abstract data types, add generic functions based on ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    Chapter 11 provides a comprehensive view of object-oriented programming (OOP) using C++: - **OOP Language Requirements:** Lists the requirements for an OOP language. - **OOP: The Dominant Programming ...

    Problem Solving with C++ (7th edition)

    This chapter introduces object-oriented programming (OOP) concepts in C++: - **Classes and Objects**: Explanation of classes and objects, including encapsulation, inheritance, and polymorphism. - **...

    Algorithms in C++, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching

    9. **First-Class ADTs**: This section introduces the concept of first-class ADTs, which are ADTs that can be treated like any other object in the language. This feature allows for greater flexibility ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...

    c++入门 txt文件

    C++中的类是实现面向对象编程的关键概念之一。例如,一个简单的栈类可以定义如下: ```cpp class stack { char s[SIZE]; // 存储数据 char* min; // 最小值指针 char* top; // 栈顶指针 char* max; // 最大值...

    C、C++日常学习.zip

    1. **面向对象**:C++引入了类(class)、对象(object)、继承(inheritance)、多态(polymorphism)等概念,支持封装、继承和多态三大面向对象特性。 2. **模板**:C++的模板允许创建泛型代码,可以用于创建泛型...

    C++泛型编程:源起、实现与意义 免积分

    早期的面向对象语言,如Java,通过使用单一基类(如`Object`)的方法来实现泛型容器,这种方式在C++中并不适用,原因在于: 1. **单根继承的限制**:C++中不强制实行单根继承,这意味着不能简单地通过所有对象继承...

    C++ How to Program (英文第8版)-高清带书签

    1. **第一个C++程序**(First Program in C++: Printing a Line of Text): - 如何编写并运行一个简单的打印语句程序。 - 理解`#include`预处理指令和`using namespace std;`的用途。 2. **修改第一个C++程序**...

    Exploring C++ 11

    Part 2: Custom Types – Introduction to Object-Oriented Programming Part 2: Custom Types – Inheritance Part 2: Custom Types – Virtual Functions Part 2: Custom Types – Classes and Types Part 2: ...

    Rad Studio Delphi C++builder XE 10.4 Patch2

    RSP-20372 A generic "reference to function" will only match the first of several overloaded functions RSP-19714 Win32 compiler - Memory corruption with array helpers RSP-18241 *.c source files, added ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX Class TPenProp rewrote without TPen delphi object. - FIX In some cases TFlexCurve.Paint leaves selected black pen in the canvas which then affects on next flex-control output (in particular ...

    C++类模板案例.zip

    在C++编程语言中,类模板是一个强大的工具,它允许我们编写通用的代码,可以处理不同类型的数据。类模板是函数模板的扩展,用于定义类而不是...通过深入理解并熟练运用这些概念,你将能够构建更加健壮和高效的C++程序。

    log4cplus 源码(C++编写的开源的日志系统)

    log4cplus是C++编写的开源的日志系统,功能非常全面,用到自己开发的工程中会比较专业的,:),本文介绍了log4cplus基本概念,以及如何安装,配置。 ### 简介 ### log4cplus是C++编写的开源的日志系统,前身是java...

    清华大学C++课件中vector用法实例

    函数对象(Function Object)是C++ STL中的一个概念,用于封装函数的调用。函数对象可以作为算法的参数,实现更加灵活地操作。下面是函数对象的使用示例: ```cpp class even_by_two { public: int operator()() ...

    Prentice.Hall.C++.for.Business.Programming.2nd.Edition.2005.chm

    Part II: Object-Oriented Programming 426 Chapter 10. The string Class: An Introduction to Classes and Objects 426 Section 10.1. Objects, Classes, and Object-Oriented Systems 427 Section 10.2. ...

Global site tag (gtag.js) - Google Analytics