Use of Loki smart pointer smartptr

Source: Internet
Author: User

Recently, I made a project that is complicated and I want to use smart pointers to manage memory. There is very little information on the Internet, and I have studied a Loki smartptr.

 

Loki Installation

1, download the source code, the latest version is the loki-0.1.7, extract.

2. compile the project under the src directory, and the generated static library is under the lib directory.

3. Add the Loki include directory to the project include directory for convenience.

Set the environment variable to loki_root = D:/loki-0.1.7/

In vs, tools-> options-> projects and solutions-> VC ++ directory, In the 'show the following content directory' drop-down to 'include files', add $ (loki_root)

4. Put the static library in a searchable path. There are two methods: adding the lib directory to the path, and copying the Lib directly to the project.

 

Installation is complete.

 

 

Smartptr usage:

Code first

Ctest. h

 

# Pragma once </P> <p> class ctest <br/> {<br/> Public: <br/> ctest (): m_id (0) {}< br/> ctest (int id): m_id (ID) {}< br/> ~ Ctest () {STD: cout <"ID:" <m_id <"-destructor is being called/N" ;}< br/> void setid (int id) {m_id = ID ;}< br/> int GETID () {return m_id ;}< br/> void dosomething () <br/> {STD :: cout <"ID:" <m_id <"-doing something/N" ;}< br/> PRIVATE: <br/> int m_id; <br/> };

Main. c

# Include <iostream> <br/> # include <vector> </P> <p> # include "Loki/smartptr. H "<br/> # include" Loki/factory. H "</P> <p> # include" test. H "</P> <p> using namespace STD; <br/> using namespace Loki; </P> <p> typedef LOKI: smartptr <ctest> testptr; </P> <p> void Pt (const testptr & T) <br/>{< br/> cout <"ID:" <t-> GETID () <Endl; <br/>}</P> <p> void main () <br/>{< br/> vector <testptr> testvector; <br/> testptr ptest0 (New ctest (0); <br/> testvector. push_back (ptest0); <br/> testptr ptest1 (New ctest (1); <br/> testvector. push_back (ptest1); <br/> for_each (testvector. begin (), testvector. end (), Pt); <br/> cout <Endl; </P> <p> Reset (ptest0, null ); // The reference count minus 1 <br/> Reset (ptest1, null); // The reference count minus 1 </P> <p> for_each (testvector. begin (), testvector. end (), Pt); <br/> cout <Endl; <br/> testvector. clear (); <br/> cout <Endl; <br/> cout <"exiting... "<Endl; <br/>}

The default template is used in the code.

 

Note:

 

Note that the default value of the 4th checking policy templates is assertcheck. That is, the release version does not check the validity only when it takes effect during the debugging period.

 

Explain the Loki Checking Policy template:

Assertcheck. By default, assert is used to check the value.

Assertcheckstrict. Use assert to check during initialization. In this way, it cannot be defined as a member variable unless the initial value is assigned.

Rejectnullstatic: use the default smartptr constructor to generate a compilation error.

Rejectnull. Each value is null. An exception occurs.

Rejectnullstrict. null is not allowed as the initial value. Otherwise, an exception occurs.

Nocheck, no template, no check.

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.