- 论坛徽章:
- 14
|
如果想影响 Create,使用户不可见,可以用static函数(不是static成员函数)- // .hpp文件
- class ItemType
- {
- public:
- static ItemType* BOOK;
- static ItemType* DVD;
- static ItemType* SOFTWARE;
- enum TYPECODE{ TYPE_BOOK, TYPE_DVD, TYPE_SOFTWARE } typecode;
- TYPECODE GetTypeCode() const
- {
- return typecode_;
- }
- private:
- TYPECODE typecode_;
- explicit ItemType( TYPECODE typecode ) : typecode_(typecode)
- {
- }
- friend ItemType* ItemType_Create( TYPECODE typecode );
- };
复制代码
- #include "a.h"
- // .cpp文件
- ItemType* BOOK = ItemType_Create(ItemType::TYPE_BOOK);
- ItemType* DVD = ItemType_Create(ItemType::TYPE_DVD);
- ItemType* SOFTWARE = ItemType_Create(ItemType::TYPE_SOFTWARE);
- static ItemType* ItemType_Create( ItemType::TYPECODE typecode )
- {
- return new ItemType( typecode );
- }
复制代码 |
|