C ++ handle parsing and handle Parsing

Source: Internet
Author: User

C ++ handle parsing and handle Parsing
C ++ handle class Parsing

Question: In C ++, identify the runtime type. Dynamically identifies the object type during running with reference or pointer in a program. However, the use of pointers or references increases the user burden (in the inheritance system, there is no clear conversion from the base class to the derived class, the user must display the conversion and add the result object to the container. However, the result is that some members of the derived object are not initialized ).

You can save the object pointer in the container to solve this problem. However, you must specify the synchronization between the pointer and the object in the container (not only the pointer, but the object does not exist or the pointer does not exist, and the object exists ).

A better solution is the handle class:

A common technology in C ++ is to define a cover class or a handle class. Handle class storage and management base class pointer. The type of the object indicated by the pointer can be changed. It can be a base class object or a derived class object. You can use the handle class to access the operation at the inheritance level. Because the handle class uses pointers to perform operations, the behavior of virtual members will change according to the type of objects actually bound to the handle at runtime. As a result, the user of the handle can obtain dynamic behavior, but there is no need to worry about pointer management.

There are two important design considerations for the handle that wraps the hierarchy:

  • Just like any class that saves pointers, you must determine what to do with replication control. A handle that wraps an inheritance level usually looks like a smart pointer or a value.
  • The handle Class determines whether the handle interface is shielded or not. If the inheritance hierarchy is not blocked, the user must understand and use the objects in the basic hierarchy.
① Pointer handle:

Handle packaging pointer. You can use the handle class as a pointer, but you do not need to manage the objects pointed to by the pointer. (The handle class is more like an intermediary Controller)

Definition scheme:

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

2. Use the class packaging pointer to wrap the counter pointer (resource and counter sharing)

 

In addition to defining constructor, copying constructor, and assigning values, the handle class also needs to define references and dereference (making it look more like a pointer)

For constructors:

The initialization member of one default constructor is 0;

One constructor declares the handle of the specified object type.

The problem arises. If the user does not know which level of object in the inheritance system is used for initialization, how can this problem be solved.

The common method to solve this problem is to define a virtual operation for copying. We call this operation clone. (Clone ):

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

class Item_base { public:     virtual Item_base* clone() const     {         return new Item_base(*this);     } };    

With the clone function, the definition of the handle class is as follows:

Sales_item::Sales_item(const Item_base &item):    p(item.clone()), use(new std::size_t(1)) { }

If logical comparison functions are required in the inheritance hierarchy, a good practice is to define internal comparison and compare internal core members.

inline boolcompare(const Sales_item &lhs, const Sales_item &rhs){    return lhs->book() < rhs->book();}

Use a comparator with an associated container

To work effectively, the associated container must use the same comparison function for each operation. However, it is unreasonable to expect the user to remember the comparison function every time. In particular, there is no way to check that each call uses the same comparison function. Therefore, it makes sense for containers to remember comparison functions. By storing the comparator in a container object, you can ensure that each operation of the comparison element is performed in a consistent manner.

In essence, this approach is to use function pointers and function callback for real comparison.

// Type of the comparison function used to order the multiset

Typedef bool (* Comp) (const Sales_item &, const Sales_item &);

Each constructor of the associated container enables us to provide the name of the comparison function. You can define the empty multiset using the compare function as follows:

Std: multiset <Sales_item, Comp> items (compare );

Multiset is the Union container in STL .. In the header file <set>, for detailed usage, see STL source code analysis.

 

Reference (C ++ primer)

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.