C++11新特性(三)Strongly-typed enums 强类型枚举简单使用


#include <iostream>

enumclass KObjectType

{

    KPlayerType,

    kMonsterType,

    kNPCType,

    KObjectTypeCount,

};



class Node

{

public:

   virtual void setType(KObjectType type) = 0;

   virtual  KObjectType getType()const = 0;

   virtual ~Node(){};

   virtual void showType() {};

};

class Player :public Node

{

public:

   virtual void setType(KObjectType type) override;

   virtual  KObjectType  getType()const override;

   virtual void showType() override {std::cout <<" PlayerType" << std::endl;};

    

private:

   KObjectType m_enType_;

};

voidPlayer::setType(KObjectType type)

{

   m_enType_ = type;

}



KObjectType  Player::getType()const

{

    returnm_enType_;

}



class Monster :public Node

{

public:

   virtual void setType(KObjectType type) override;

   virtual  KObjectType  getType()const override;

   virtual void showType() override {std::cout <<" MonsterType" << std::endl;};


private:

   KObjectType m_enType_;



};



void Monster::setType(KObjectType type)

{

   m_enType_ = type;

}



KObjectType  Monster::getType()const

{

    returnm_enType_;

}



class FactoryCreation

{

    

public:

   static Node* create(KObjectType type);

    

};


Node* FactoryCreation::create(KObjectType type)

{

   Node *object = nullptr;

    

   switch (type) {

            

       case KObjectType::KPlayerType:

            object =new Player();

           break;


       case KObjectType::kMonsterType:

            object =new Monster();

           break;

       default:

           break;

    }

   if (object) {

        object->setType(type);

        

    }

    

   return object;

}


int main()

{

    

    auto p =FactoryCreation::create(KObjectType::KPlayerType);

    p->showType();

   delete p;

    

    

    auto m =FactoryCreation::create(KObjectType::kMonsterType);

    m->showType();

   delete m;    

   return 0;

}


结果输出如下:

PlayerType

 MonsterType

Program ended with exit code: 0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值