C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。
摘自网上的一段话:
需要句柄类的背景:
1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息
2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。 句柄类就是为了解决这类问题
比如,我有一个类Person,在很多地方都用到了这个类(#incude Person.h),在程序不断完善的过程中,难免会对该类进行重新设计或者一些修改。当Person.h被很多文件包含时,即使我们只是对Person类做了很小的改动,那么所有包含该头文件的文件,都需要重新编译,这会花费大量的无谓的时间,我想这是每一个编程人员都不希望看到的,为了解决此类问题的发生,我们便想到了句柄类。
设计思想:
我们把Person类设计成一个 handle class (句柄类),而重新定义一个新类 PersonImpl,作为具体的实现类,当然原本Person类中的成员变量,和成员函数的具体实现都放在PersonImpl类中。在Person类中定义一个指向PersonImpl类的指针,用于调用具体的实现。这样一来,当实现修改了,我们只需要重新编译Person文件即可。
具体代码如下:
- //Person.h
- //the handle class
- //定义
- #include <string>
- #include <memory>
- class PersonImpl;
- class Person
- {
- public:
- Person(const std::string& name,const int &id,const std::string& addr);
- std::string rename() const;
- int reid() const;
- std::string readdr() const;
- private:
- std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向具体的完成实现工作的类 PersonImpl
- };
- //Person.cpp
- //the handle class
- //实现
- #include "Person.h"
- #include "PersonImpl.h" //必须这样做,否则无法调用其成员函数,
- //注意,PersonImpl有着和句柄类Person完全相同的成员函数,2者接口完全相同
- Person::Person(const std::string& name,const int &id,const std::string& addr)
- :pImpl(new PersonImpl(name,id,addr))
- {
- }
- std::string Person::rename() const
- {
- return pImpl->rename(); //通过指针调用实现类里的成员函数,完成具体动作
- }
- int Person::reid() const
- {
- return pImpl->reid();
- }
- std::string Person::readdr() const
- {
- return pImpl->readdr();
- }
- //PersonImpl.h
- //the implement class
- //定义
- #include <string>
- #include <memory>
- class PersonImpl
- {
- public:
- PersonImpl(const std::string& name,const int &id,const std::string& addr);
- std::string rename() const;
- int reid() const;
- std::string readdr() const;
- private:
- std::string theName;
- int theId;
- std::string theAddress;
- };
- //PersonImpl.cpp
- //the implement class
- //实现
- #include "PersonImpl.h"
- PersonImpl::PersonImpl(const std::string& name,const int &id,const std::string& addr)
- :theName(name),theId(id),theAddress(addr)
- {
- }
- std::string PersonImpl::rename() const
- {
- return theName;
- }
- int PersonImpl::reid() const
- {
- return theId;
- }
- std::string PersonImpl::readdr() const
- {
- return theAddress;
- }
- //Main.cpp
- #include <iostream>
- #include "Person.h"
- //主程序,只需要包含Person类,因为Person类的接口永远不会改变,如果实现文件PersonImpl中的接口或者成员改变了
- //只需要重新编译Person.h,而不需要重新编译包含person.h的文件了
- int main()
- {
- Person man1("Tom",10,"England");
- std::cout<<"the name:"<<man1.rename()<<std::endl;
- std::cout<<"the id:"<<man1.reid()<<std::endl;
- std::cout<<"the Address:"<<man1.readdr()<<std::endl;
- system("pause");
- return 0;
- }
转自:
C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。
摘自网上的一段话:
需要句柄类的背景:
1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息
2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。 句柄类就是为了解决这类问题
比如,我有一个类Person,在很多地方都用到了这个类(#incude Person.h),在程序不断完善的过程中,难免会对该类进行重新设计或者一些修改。当Person.h被很多文件包含时,即使我们只是对Person类做了很小的改动,那么所有包含该头文件的文件,都需要重新编译,这会花费大量的无谓的时间,我想这是每一个编程人员都不希望看到的,为了解决此类问题的发生,我们便想到了句柄类。
设计思想:
我们把Person类设计成一个 handle class (句柄类),而重新定义一个新类 PersonImpl,作为具体的实现类,当然原本Person类中的成员变量,和成员函数的具体实现都放在PersonImpl类中。在Person类中定义一个指向PersonImpl类的指针,用于调用具体的实现。这样一来,当实现修改了,我们只需要重新编译Person文件即可。
具体代码如下:
- //Person.h
- //the handle class
- //定义
- #include <string>
- #include <memory>
- class PersonImpl;
- class Person
- {
- public:
- Person(const std::string& name,const int &id,const std::string& addr);
- std::string rename() const;
- int reid() const;
- std::string readdr() const;
- private:
- std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向具体的完成实现工作的类 PersonImpl
- };
- //Person.cpp
- //the handle class
- //实现
- #include "Person.h"
- #include "PersonImpl.h" //必须这样做,否则无法调用其成员函数,
- //注意,PersonImpl有着和句柄类Person完全相同的成员函数,2者接口完全相同
- Person::Person(const std::string& name,const int &id,const std::string& addr)
- :pImpl(new PersonImpl(name,id,addr))
- {
- }
- std::string Person::rename() const
- {
- return pImpl->rename(); //通过指针调用实现类里的成员函数,完成具体动作
- }
- int Person::reid() const
- {
- return pImpl->reid();
- }
- std::string Person::readdr() const
- {
- return pImpl->readdr();
- }
- //PersonImpl.h
- //the implement class
- //定义
- #include <string>
- #include <memory>
- class PersonImpl
- {
- public:
- PersonImpl(const std::string& name,const int &id,const std::string& addr);
- std::string rename() const;
- int reid() const;
- std::string readdr() const;
- private:
- std::string theName;
- int theId;
- std::string theAddress;
- };
- //PersonImpl.cpp
- //the implement class
- //实现
- #include "PersonImpl.h"
- PersonImpl::PersonImpl(const std::string& name,const int &id,const std::string& addr)
- :theName(name),theId(id),theAddress(addr)
- {
- }
- std::string PersonImpl::rename() const
- {
- return theName;
- }
- int PersonImpl::reid() const
- {
- return theId;
- }
- std::string PersonImpl::readdr() const
- {
- return theAddress;
- }
- //Main.cpp
- #include <iostream>
- #include "Person.h"
- //主程序,只需要包含Person类,因为Person类的接口永远不会改变,如果实现文件PersonImpl中的接口或者成员改变了
- //只需要重新编译Person.h,而不需要重新编译包含person.h的文件了
- int main()
- {
- Person man1("Tom",10,"England");
- std::cout<<"the name:"<<man1.rename()<<std::endl;
- std::cout<<"the id:"<<man1.reid()<<std::endl;
- std::cout<<"the Address:"<<man1.readdr()<<std::endl;
- system("pause");
- return 0;
- }
http://blog.csdn.net/zicheng_lin/article/details/6317271
C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。
摘自网上的一段话:
需要句柄类的背景:
1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息
2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。 句柄类就是为了解决这类问题
比如,我有一个类Person,在很多地方都用到了这个类(#incude Person.h),在程序不断完善的过程中,难免会对该类进行重新设计或者一些修改。当Person.h被很多文件包含时,即使我们只是对Person类做了很小的改动,那么所有包含该头文件的文件,都需要重新编译,这会花费大量的无谓的时间,我想这是每一个编程人员都不希望看到的,为了解决此类问题的发生,我们便想到了句柄类。
设计思想:
我们把Person类设计成一个 handle class (句柄类),而重新定义一个新类 PersonImpl,作为具体的实现类,当然原本Person类中的成员变量,和成员函数的具体实现都放在PersonImpl类中。在Person类中定义一个指向PersonImpl类的指针,用于调用具体的实现。这样一来,当实现修改了,我们只需要重新编译Person文件即可。
具体代码如下:
- //Person.h
- //the handle class
- //定义
- #include <string>
- #include <memory>
- class PersonImpl;
- class Person
- {
- public:
- Person(const std::string& name,const int &id,const std::string& addr);
- std::string rename() const;
- int reid() const;
- std::string readdr() const;
- private:
- std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向具体的完成实现工作的类 PersonImpl
- };
- //Person.cpp
- //the handle class
- //实现
- #include "Person.h"
- #include "PersonImpl.h" //必须这样做,否则无法调用其成员函数,
- //注意,PersonImpl有着和句柄类Person完全相同的成员函数,2者接口完全相同
- Person::Person(const std::string& name,const int &id,const std::string& addr)
- :pImpl(new PersonImpl(name,id,addr))
- {
- }
- std::string Person::rename() const
- {
- return pImpl->rename(); //通过指针调用实现类里的成员函数,完成具体动作
- }
- int Person::reid() const
- {
- return pImpl->reid();
- }
- std::string Person::readdr() const
- {
- return pImpl->readdr();
- }
- //PersonImpl.h
- //the implement class
- //定义
- #include <string>
- #include <memory>
- class PersonImpl
- {
- public:
- PersonImpl(const std::string& name,const int &id,const std::string& addr);
- std::string rename() const;
- int reid() const;
- std::string readdr() const;
- private:
- std::string theName;
- int theId;
- std::string theAddress;
- };
- //PersonImpl.cpp
- //the implement class
- //实现
- #include "PersonImpl.h"
- PersonImpl::PersonImpl(const std::string& name,const int &id,const std::string& addr)
- :theName(name),theId(id),theAddress(addr)
- {
- }
- std::string PersonImpl::rename() const
- {
- return theName;
- }
- int PersonImpl::reid() const
- {
- return theId;
- }
- std::string PersonImpl::readdr() const
- {
- return theAddress;
- }
- //Main.cpp
- #include <iostream>
- #include "Person.h"
- //主程序,只需要包含Person类,因为Person类的接口永远不会改变,如果实现文件PersonImpl中的接口或者成员改变了
- //只需要重新编译Person.h,而不需要重新编译包含person.h的文件了
- int main()
- {
- Person man1("Tom",10,"England");
- std::cout<<"the name:"<<man1.rename()<<std::endl;
- std::cout<<"the id:"<<man1.reid()<<std::endl;
- std::cout<<"the Address:"<<man1.readdr()<<std::endl;
- system("pause");
- return 0;
- }