C++ 原型模式C+中的智能指针+;
我目前正在查看我在github上找到的一些代码,但是我对代码的某些部分以及它的作用感到非常困惑 下面这行是什么意思?键是一个C++ 原型模式C+中的智能指针+;,c++,c++11,smart-pointers,C++,C++11,Smart Pointers,我目前正在查看我在github上找到的一些代码,但是我对代码的某些部分以及它的作用感到非常困惑 下面这行是什么意思?键是一个bulletType,值是一个智能指针,什么是hash 主要代码: #include <iostream> #include <unordered_map> #include <string> #include <memory> using namespace std; /** Bullet is the base Pro
bulletType
,值是一个智能指针,什么是hash
主要代码:
#include <iostream>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
/** Bullet is the base Prototype */
class Bullet
{
protected:
string _bulletName;
float _speed;
float _firePower;
float _damagePower;
float _direction;
public:
Bullet(){}
Bullet(string bulletName, float speed, float firePower, float damagePower)
: _bulletName(bulletName), _speed(speed), _firePower(firePower), _damagePower(damagePower)
{}
virtual ~Bullet() {}
virtual unique_ptr<Bullet> clone() = 0;
void fire(float direction)
{
_direction = direction;
cout << "Name : " << _bulletName << endl
<< "Speed : " << _speed << endl
<< "FirePower : " << _firePower << endl
<< "DamagePower : " << _damagePower << endl
<< "Direction : " << _direction << endl << endl;
}
};
/** SimpleBullet is a Concrete Prototype */
class SimpleBullet : public Bullet
{
public:
SimpleBullet(string bulletName, float speed, float firePower, float damagePower) :
Bullet(bulletName, speed, firePower, damagePower)
{
}
unique_ptr<Bullet> clone() override
{
return make_unique<SimpleBullet>(*this);
}
};
/** GoodBullet is the Concrete Prototype */
class GoodBullet : public Bullet
{
public:
GoodBullet(string bulletName, float speed, float firePower, float damagePower)
: Bullet(bulletName, speed, firePower, damagePower)
{
}
unique_ptr<Bullet> clone() override
{
return std::make_unique<GoodBullet>(*this);
}
};
/** Opaque Bullet type, avoids exposing concrete implementations */
enum BulletType
{
SIMPLE,
GOOD
};
/** BulletFactory is the client */
class BulletFactory
{
private:
unordered_map<BulletType, unique_ptr<Bullet>, hash<int> > m_Bullets;
public:
BulletFactory()
{
m_Bullets[SIMPLE] = make_unique<SimpleBullet>("Simple Bullet", 50, 75, 75);
m_Bullets[GOOD] = make_unique<GoodBullet>("Good Bullet", 75, 100, 100);
}
unique_ptr<Bullet> createBullet(BulletType BulletType)
{
return m_Bullets[BulletType]->clone();
}
};
int main()
{
BulletFactory bulletFactory;
auto Bullet = bulletFactory.createBullet(SIMPLE);
Bullet->fire(90);
Bullet = bulletFactory.createBullet(GOOD);
Bullet->fire(100);
}
#包括
#包括
#包括
#包括
使用名称空间std;
/**Bullet是基本原型*/
等级子弹
{
受保护的:
字符串bulletName;
浮动速度;
漂浮火力;
浮子损伤功率;
浮动方向;
公众:
项目符号(){}
子弹(字符串bulletName、浮动速度、浮动火力、浮动伤害力)
:\u bulletName(bulletName),\u speed(speed),\u firePower(firePower),\u damagePower(damagePower)
{}
虚拟~Bullet(){}
虚拟唯一_ptr clone()=0;
空火(浮动方向)
{
_方向=方向;
couthash
是一个函数,用于使未排序映射(实际上是-hash映射)的键成为可以查询(并与进行比较)的内容
return std::make_unique(*this)
:
构造一个GoodBullet
(在本例中,使用copy-c'tor)
将其包装在std::unique\u ptr
中,然后
归还它
std::unique_ptr
是一个智能指针,它不允许对其进行复制,因此您知道从clone()
获得的实例是唯一的实例(除非您从预先存在的原始指针构建了一个unique_ptr
),您只能“移动”它是另一个unique\u ptr
,当它超出范围时,它也会破坏包装好的GoodBullet
。关于你关于std::make\u unique
的问题,这里有一个很好的答案:关于你第一个关于什么是hash
的问题,也许这有帮助:当你说它包装在std::uni这实际上只是将地址返回给新创建的对象吗?不是。它通过值返回一个对象,该对象封装了指向该地址的指针。
return std::make_unique<GoodBullet>(*this)
#include <iostream>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
/** Bullet is the base Prototype */
class Bullet
{
protected:
string _bulletName;
float _speed;
float _firePower;
float _damagePower;
float _direction;
public:
Bullet(){}
Bullet(string bulletName, float speed, float firePower, float damagePower)
: _bulletName(bulletName), _speed(speed), _firePower(firePower), _damagePower(damagePower)
{}
virtual ~Bullet() {}
virtual unique_ptr<Bullet> clone() = 0;
void fire(float direction)
{
_direction = direction;
cout << "Name : " << _bulletName << endl
<< "Speed : " << _speed << endl
<< "FirePower : " << _firePower << endl
<< "DamagePower : " << _damagePower << endl
<< "Direction : " << _direction << endl << endl;
}
};
/** SimpleBullet is a Concrete Prototype */
class SimpleBullet : public Bullet
{
public:
SimpleBullet(string bulletName, float speed, float firePower, float damagePower) :
Bullet(bulletName, speed, firePower, damagePower)
{
}
unique_ptr<Bullet> clone() override
{
return make_unique<SimpleBullet>(*this);
}
};
/** GoodBullet is the Concrete Prototype */
class GoodBullet : public Bullet
{
public:
GoodBullet(string bulletName, float speed, float firePower, float damagePower)
: Bullet(bulletName, speed, firePower, damagePower)
{
}
unique_ptr<Bullet> clone() override
{
return std::make_unique<GoodBullet>(*this);
}
};
/** Opaque Bullet type, avoids exposing concrete implementations */
enum BulletType
{
SIMPLE,
GOOD
};
/** BulletFactory is the client */
class BulletFactory
{
private:
unordered_map<BulletType, unique_ptr<Bullet>, hash<int> > m_Bullets;
public:
BulletFactory()
{
m_Bullets[SIMPLE] = make_unique<SimpleBullet>("Simple Bullet", 50, 75, 75);
m_Bullets[GOOD] = make_unique<GoodBullet>("Good Bullet", 75, 100, 100);
}
unique_ptr<Bullet> createBullet(BulletType BulletType)
{
return m_Bullets[BulletType]->clone();
}
};
int main()
{
BulletFactory bulletFactory;
auto Bullet = bulletFactory.createBullet(SIMPLE);
Bullet->fire(90);
Bullet = bulletFactory.createBullet(GOOD);
Bullet->fire(100);
}