Working with object pointers in STL

Source: Internet
Author: User
Tags bool

As we all know, the STL container class is for the object to store management. Although there is no clear limit to the inclusion of pointers in the STL container class, the STL does not care about the objects or pointers that you store in the container. But carefully consider the original intention of STL, the use of pointers in the container class is obviously inappropriate.

Cker means that you should try to place the object itself rather than the object's pointer in the STL container class as much as possible. One of the biggest side effects of holding pointers directly in a container is that it can cause a memory leak. This problem is highlighted in the char * type.

At some point, however, the direct use of object pointers has obvious benefits, which can generally be summed up in several areas as follows:

1. When the object is very large, the frequent copying of the system resources is very expensive to consume.

2. This should not be uncommon when you need to store the same object in multiple containers.

3. When you need to store multiple derived class objects that derive from the same parent class in the same container. This is also very common.

In fact, Cker is facing a third situation in the computing program developed this week. Consider the various benefits of using STL.

Cker decided to introduce the STL list container.

Originally, the use of BCB tlist objects can also achieve the same purpose.

But

The Tlsit class has a significant decrease in efficiency when the number of object pointers exceeds 5,000.

The Tlist class is not type-safe, and it doesn't care what kind of object pointers are introduced.

Introducing the Tlist class means that you want to include the VCL.h header file, which is not a good thing for the portability of my computing modules.

Cker made a decision, facing two STL-related problems.

The first question is the above mentioned how to handle objects in the STL.

The Cker solution is to create a class that encapsulates pointers.

The code is as follows:

//Define a pointer encapsulation class for STL containers
//Because it is not appropriate to add pointers directly to the container using STL.
////////////////////////////////////////////////////////////////////////////////
Class PtrWrapper
{
Private:
x* px;//pointer to class X
Public:
file://construct and copy constructor
Ptrwrapper (x* x = 0): px (x) {}
Ptrwrapper (cons T ptrwrapper& PW): px (pw.px) {}
file://destructor
~ptrwrapper () {}
ptrwrapper& operator= (const ptrwrapper& Amp PW) {px = xw.px;}
file://overloaded operator () returns the pointer to object X
Const x* operator () () const {return px;}
x* operator () () {return px;}
};
file://overloaded logical operator = = <
bool operator== (const ptrwrapper& PW1, const ptrwrapper& pw2) {
Retu RN (Pw1.operator () () && Pw2.operator () ())? *PW1 () = = *pw2 (): false;
}
bool operator< (const ptrwrapper& PW1, const ptrwrapper& pw2) {
return (PW1 () && pw2 ()) ? *PW1 () < *PW2 (): false;
}
bool Operator> (const ptrwrapper& PW1, const ptrwrapper& pw2) {
ReturN (PW1 () && pw2 ())?! (*PW1 () < *PW2 ()): false;
}

The code above encapsulates a pointer. After the encapsulation of the class Ptrwrapper, you do not need to use the pointer directly.

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.