Principle and Design of the C ++ handle class

Source: Internet
Author: User

Principle and Design of the C ++ handle class
The processing class is used to make up for the slicing effect that occurs when the derived class object is assigned to the Base Class Object. For example, the following program:

multimap<Base> basket;Base base;Derived derive;basket.insert(base);//ok,add copy of base;basket.insert(derive);//ok,but derive sliced down to its base part.

That is to say, when the object of the derived class is assigned to the Base class, the slice benefit will occur, and the non-base class part of the derived class will be cut off, which will lose its meaning. To solve this problem, we can
The base class-based pointer or reference is used. However, when the pointer is designed, the release of resources after the resource is not used is involved. This leads to the handle class, which is similar to a smart pointer and can be used to copy resources.
You don't have to worry about memory leakage. The entire program is designed as follows:
//Base.h#pragma onceclass Base{public:Base(void);virtual ~Base(void);virtual Base* clone() const;virtual void fine() const;private:int mb;};

//Base.cpp#include "Base.h"#include <iostream>using namespace std;Base::Base(void):mb(12){}Base::~Base(void){}Base* Base::clone() const{return new Base(*this);}void Base::fine() const{cout<<"Base fine function"<<endl;}

//Derive.h#pragma once#include "base.h"class Derive :public Base{public:Derive(void);virtual ~Derive(void);virtual Derive* clone() const;virtual void fine() const;private:int md;};

//Derive.cpp#include "Derive.h"#include <iostream>using namespace std;Derive::Derive(void):Base(),md(13){}Derive::~Derive(void){}Derive* Derive::clone() const{return new Derive(*this);}void Derive::fine() const{cout<<"Derive fine function"<<endl;}

//Handles.h #pragma once#include "Base.h"#include <iostream>class Handles{public:Handles(void);Handles(const Base&);Handles(const Handles& h);~Handles(void);const Base* operator->()const;const Base& operator*()const;Handles& operator=(const Handles&);private:Base*p;std::size_t*use;void dec_use(){if(--*use == 0){delete p;delete use;std::cout<<"delete resource"<<std::endl;}}};

//Handle.cpp #include "Handles.h"Handles::Handles(void):p(NULL),use(new size_t(1)){}Handles::Handles(const Handles& h):p(h.p),use(h.use){++*use;}Handles::Handles(const Base& item):p(item.clone()),use(new std::size_t(1)){}const Base& Handles::operator*()const{if(p) return *p;elsethrow std::logic_error("unbounded Handles");}const Base* Handles::operator->()const{if(p) return p;elsethrow std::logic_error("unbounded Handles");}Handles& Handles::operator=(const Handles& h){++*h.use;dec_use();p = h.p;use = h.use;return *this;}Handles::~Handles(){dec_use();}

//main.cpp#include <iostream>#include "Handles.h"#include "Derive.h"#include <vector>using namespace std;void main(){vector<Handles> mb;Base b;Derive d;mb.push_back(Handles(b));mb.push_back(Handles(d));mb[0]->fine();mb[1]->fine();system("pause");} 




What is the handle class in c ++?

C ++ does not have the so-called handle class concept.

In windows programming, there is a HANDLE concept. The type is HANDLE, which is not a class. In windows, HANDLE is actually just a type definition, the internal type is Numeric (such as int). This is only to uniquely identify an object/kernel object.

What is the principle of C programming?

Let's take a look at the assembly and computer composition principles.
Then look at c.
You will know a lot of things.
Just look at the mighty C.
The real thing is far away.

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.