C + + handle parsing

Source: Internet
Author: User

C + + Handle class parsing

Introduction: In C + +, for run-time type recognition issues. Use references in your program or pointers to dynamically identify object types at run time. However, using pointers or references increases the burden on the user (in the inheritance system, there is no explicit conversion of the base class to the derived class, and the user must display the transformation and add the resulting object to the container.) However, the result is that some members of the derived object are uninitialized.

For this problem, you can save the object pointer in a container to resolve it. At this point, however, the user must be clear about the synchronization of pointers and objects in the container (not only the pointer but the object does not exist or the pointer does not exist, the object exists).

The better solution is the handle class:

A common technique in C + + is to define wrapper (cover) classes or handle classes. Handle classes Store and manage base class pointers. The type of the object that the pointer refers to can vary, which can point to both the base class type object and the derived type object. The user accesses the inheritance hierarchy operation through the handle class. Because a handle class uses pointers to perform operations, the behavior of a virtual member changes at run time based on the type of object the handle is actually bound to. Therefore, the user of the handle can get dynamic behavior without worrying about the management of pointers.

There are two important design considerations for wrapping a handle to an inheritance hierarchy:

    • As with any class that holds pointers, you must determine what to do with replication control. A handle that wraps an inheritance hierarchy usually behaves like a smart pointer or a value.
    • The handle class determines whether the handle interface masks or does not mask the inheritance hierarchy, and if the inheritance hierarchy is not masked, the user must understand and use the objects in the base hierarchy.
① pointer-type handle:

The handle wraps a pointer that the user can use as a pointer, without having to manage the object pointed to by the pointer. (The handle class is more like a mediation controller)

Define the scenario:

1. Use the class wrapper pointer to wrap the counter (each object has its own two members)

2. Using class wrapper pointers, wrapping counter pointers (resource and counter sharing)

In addition to defining constructs, copying constructs, assigning values, you need to define references, and dereference them (making them look more like pointers).

For constructors:

1 default constructor initializes a member of 0;

1 constructors declare a handle to the specified object type.

So the question is, if the user does not know what level of object in the inheritance system to initialize, how to do it.

The common way to solve this problem is to define a virtual operation for replication, which we call named Clone. (clone):

For each class in the inheritance hierarchy, add a virtual clone function eg:

Classpublic:     virtualconst     {         return  New item_base (*this);    };

With a clone function, the handle class is defined as follows:

Sales_item::sales_item (const item_base &Item):    p (Item.clone ()), use (new std::size_t (1)) {}

For a logical comparison function in an inheritance hierarchy, a good practice is to define internal comparisons and compare internal core members.

BOOL Compare (constconst Sales_item &rhs) {    return Lhs->book () < rhs-> book();}

Using a comparer with associative containers

To work effectively, the associative container needs to use the same comparison function for each operation. However, it is unreasonable to expect the user to remember the comparison function every time, and in particular, there is no way to check each call using the same comparison function. Therefore, it makes sense for the container to remember the comparison function. By storing the comparer in a container object, you can guarantee that each operation of the comparison element will proceed consistently.

In essence, this is done by using function pointers and function callbacks to achieve real comparisons.

Type of the comparison function used to order the Multiset

typedef BOOL (*COMP) (const sales_item&, const sales_item&);

Each constructor of the associative container allows us to provide the name of the comparison function. You can define an empty multiset that uses the Compare function:

Std::multiset<sales_item, comp> items (compare);

Multiset is a federated container in the STL. In the header file <set>, please refer to "STL Source code Anatomy" for specific usage.

Reference (C++primer)

C + + handle parsing

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.