Value semantics (Chen Shuo)

Source: Internet
Author: User

The value semantics indicates that copying an object has nothing to do with the object metadata, just like copying an int. The built-in types of C ++ (bool/INT/Double/Char) are both value semantics, complex <>, pair <>, vector <>, map <> and so on are both value semantics. After copying, it is out of relationship with the original object.

The object semantics corresponding to the value semantics, or the reference semantics. The object semantics refer to objects in the object-oriented sense, and object copying is prohibited.

 

Value semantics and life cycle

A huge benefit of value semantics is that life cycle management is very simple. Just like int, you don't need to worry about the life cycle of Int. The value semantics object is either a stack object, or directly as a member of other objects, so we don't need to worry about its lifecycle (a function uses the objects on its own stack, and a member function uses its own data member objects, on the contrary, because the object of object semantics cannot be back to back, we can only use it by correcting or referencing it.

Once you use pointers and references to operate on objects, you need to worry about whether the objects to be pointed to have been released. This was once a major source of C ++ program bugs. In addition, since C ++ can only obtain polymorphism through pointers or references, it is essential for C ++ to engage in inheritance-based and peptide-Based Object-Oriented Programming-Resource Management

If a pointer is used as a member directly, how can we ensure the validity of the pointer, how to prevent the occurrence of a null pointer, and how to ensure that the program has no ambition for its pointer when releasing an object...

This series of problems was once a headache in C ++ object-oriented programming, but we can use smart point to convert object semantics into value semantics, so as to easily solve the object life cycle.

 

What is data abstraction?

Data abstraction is an object-oriented programming paradigm, that is, an abstract data type.

Supporting data abstraction has always been the design goal of C ++.

 

In short, data abstraction is used to describe the data structure, and data abstraction is ADT. An ADT mainly shows some operations supported by it, such as stack. push, stack. pop groups should have clear time and space complexity. In addition, an ADT can conceal its implementation details. For example, stack can be implemented using dynamic arrays or linked lists.

According to this definition, data abstraction is similar to object-based. where are their differences? The semantics is different. ADT is usually the value semantics, and Object-based is the object language, the ADT class can be copied. The copied instance is out of relationship with the original Instance.

Data abstraction in the C ++ standard library

Complex <>, pair <>, vector <>, list <>, Map <>, set <>, String, stack, and queue in the C ++ standard library are examples of data abstraction. Vector is a dynamic array. Its main operations include push_back (), size (), begin (), and end (). These operations not only have clear meanings, but also have constant computing complexity. Similarly, list is a linked list, map is an ordered Association array, set is an ordered collection, stack is a filo stack, and queue is a FIFO queue. "Dynamic Array", "linked list", "ordered set", "associated array", "stack", and "queue" are all abstract data types with clear definitions (operations and complexity.

Differences between data abstraction and object-oriented

This article regards data processing action, Object-based, and object-oriented as three programming paradigms. This meticulous classification may help you understand and differentiate the differences between them.

In layman's terms, object-oriented has three features: encapsulation, inheritance, and polypeptide. Object-based, there are only encapsulation, no inheritance, and multiple types, that is, there are only specific classes and no abstract interfaces, both of them are object semantics. The true core idea of object-oriented is message transmission. encapsulation and polymorphism are just representations.

The boundaries between data abstraction and them are semantics. Data abstraction is not object semantics, but value semantics.

Data abstraction is for data, which means that the ADT class can be copied, as long as the data is copied. If a class represents other resources, it is object-based or object-oriented, rather than data abstraction.

 

Language facilities required for data abstraction

Not all languages support data abstraction

Support data aggregation

Data aggregation, or value aggregates. Define c-style struct and put the relevant data in the same struct.

 

Global functions and overloading

For example, if I define complex, I can define complex sin (const complex & X); and complex exp (const complex & X ); and so on. Sin and EXP are not members of complex, but the global functions of double sin (double) and double exp (double. This allows double A = sin (B); and complex A = sin (B); to have the same code form, without having to write complex A = B. Sin ();.

 

Member functions and private data

Data can be declared as private to prevent accidental modification. Not every ADT is suitable for declaring data as private.

 

Copy Control

 

Operator overload

 

Templates and generics

 

Value semantics (Chen Shuo)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.