Handle Class(读书笔记)

来源:互联网 发布:sqlserver union join 编辑:程序博客网 时间:2024/02/24 04:01
When do we need a handle class?
      If we want to hide the whole information about a class, we need to define a handle
class to wrap it. We just declare the class in the handle class, and implement it in another
file.
Merits:
    1, hide the implemetation
    2, when we modified Cheshire, what we need to do is to recompile handle.cpp and link
the object file to the project. If we don't use this mechnism, we would to build all the file
that include the class's head file, which we have modified;
Just as follow:

//handle.h
//Handle classes
#ifndef HANDLE_H
#define HANDLE_H

class Handle
{
    struct Cheshire;    //class declaration only
    Cheshire *smile;
public:
    void initialize();
    void cleanup;
    int read();
    void change(int);   
};

#endif    //HANDLE_H   

//handle.cpp
//Handle implementation
#include "handle.h"

//define Handle's implemetation:

struct Handle::Cheshire
{
    int i;
};

void Handle::initialize()
{
    smile = new Cheshire;
    smile->i = 0;
}

void Handle::cleanup()
{
    delete smile;
}

int Handle::read()
{
    return smile->i;
}

void Handle::change(int x)
{
    smile->i = x;
}
原创粉丝点击