在推入向量时调用方法

时间:2016-08-20 18:46:53

标签: c++

我现在正在使用数组:

weapons = new Weapon[nWeapon];

然后我在添加时从另一个类调用方法:

weapons[0].createWeapon("Sword",1,2,3);

我想将其重写为vector,但在调用vector时,我不知道如何在push_back中调用该方法。

修改(已添加代码):

Weapon.h

#pragma once
#include "Item.h"

class Weapon : public Item {
public:
    Weapon();
    Weapon(Item i, int d);
    Weapon(string n, int c, int l, int d);
    Weapon(const Weapon* otherWeapon);
    Weapon(const Weapon& orig);
    virtual ~Weapon();

    void CreateWeapon(string n, int c, int l, int d);

    int GetDamage() const;
    void SetDamage(int d);

    void DisplayInfo();
private:
    int damage;
};

Weapon.cpp

#include <iostream>
using namespace std;

#include "Weapon.h"

Weapon::Weapon() {
    damage = 0;
    SetType(Item::Weapon);
}

Weapon::Weapon(const Weapon* otherWeapon) {
    SetName(otherWeapon->GetName());
    SetCost(otherWeapon->GetCost());
    SetLevel(otherWeapon->GetLevel());
    SetType(Item::Weapon);
    damage = otherWeapon->GetDamage();
}

Weapon::Weapon(const Weapon& orig) {
    SetName(orig.GetName());
    SetCost(orig.GetCost());
    SetLevel(orig.GetLevel());
    SetType(orig.GetType());
    damage = orig.GetDamage();
}

Weapon::Weapon(Item i, int d) : Item(i) {
    damage = d;
    SetType(Item::Weapon);
}

Weapon::Weapon(string n, int c, int l, int d) : Item(n, c, l) {
    damage = d;
    SetType(Item::Weapon);
}

Weapon::~Weapon() {
}

int Weapon::GetDamage() const {
    return damage;
}

void Weapon::SetDamage(int d) {
    if (d > 0)
        damage = d;
}

void Weapon::CreateWeapon(string n, int c, int l, int d) {
    SetName(n);
    SetCost(c);
    SetLevel(l);
    SetDamage(d);
    SetType(Item::Weapon);
}

void Weapon::DisplayInfo() {
    Item::DisplayInfo();
    cout << ", Damage: " << damage;
}

4 个答案:

答案 0 :(得分:0)

成员函数CreateWeapon完全是多余的,对于任何可能维护代码的人来说,它的名字都是噩梦。

首先,摆脱CreateWeapon

其次,您可以使用std::vector这样使用它:

std::vector<Weapon> weapons_vector;
weapons_vector.reserve(nWeapon); //this allocates memory but does not initialize (does not call the constructor).

现在,当您想要创建新的Weapon

weapons_vector.emplace_back("Sword",1,2,3);

如果无法使用emplace_back

weapons_vector.push_back(Weapon("Sword",1,2,3));

答案 1 :(得分:0)

你的功能,createweapon已经混淆了,因为它明确地创建武器,它只是填充已经存在的武器。

这是成员函数,这意味着您必须在现有对象(Waepon w; w.CreateWeapon(...))的实例上调用它 - 这是另一个可怕名称的原因。

在这种情况下,有三种方法可以使用push_back:

明智的C ++ 11方式:

std::vector<Weapon> weapons;
weapons.emplace_back("sword", 50, 1, 5);

emplace_back此处将参数传递给武器的构造函数

明智的前C ++ 11方式:

std::vector<Weapon> weapons;
weapons.push_back(Weapon("sword", 50, 1, 5));

或者你可以向世界展示你讨厌生活并以这种方式去做:

std::vector<Weapon> weapons;
Weapon weapon;
weapon.createWeapon("sword", 50, 1, 5);
weapons.push_back(weapon);

如果你不完全摆脱这个功能,请重命名&#39; InitializeWeapon&#39;?

完整示例:

#include <vector>
#include <string>
#include <iostream>

enum class ItemType {
    Weapon
};

struct Item {
    Item(std::string name, int cost, int level) : name_(name), cost_(cost), level_(level) {}
    virtual ~Item() {}

    void SetType(ItemType type) { type_ = type; }
    virtual void DisplayInfo() const {
        std::cout << name_ << ", " << cost_ << "c, L" << level_;
    }

    std::string name_;
    int cost_;
    int level_;
    ItemType type_;
};

struct Weapon : public Item {
    Weapon(std::string name, int cost, int level, int damage) : Item(name, cost, level), damage_(damage)
    {
        SetType(ItemType::Weapon);
    }

    virtual void DisplayInfo() const override {
        Item::DisplayInfo();
        std::cout << ", D" << damage_;
    }

    int damage_;
};

int main() {
    std::vector<Weapon> weapons;
    weapons.emplace_back("sword", 50, 1, 2);
    weapons.push_back(Weapon("dagger", 60, 2, 3));

    for (auto& it : weapons) {
        it.DisplayInfo();
        std::cout << '\n';
    }

    std::cout << "weapons[0]: ";
    weapons[0].DisplayInfo();
    std::cout << "\n";
    return 0;
}

http://ideone.com/ZyqVUu

答案 2 :(得分:-1)

// create new vector and fill it with nWeapon items (initialized with Weapon default constructor)
std::vector<Weapon> weapons(nWeapon);
// call method of weapon at index 0
weapons.at(0).createWeapon("Sword",1,2,3);
// or "direct access" without bound checking:
weapons[0].createWeapon("Sword",1,2,3);

如果你想从空矢量开始,然后逐个填充它:

std::vector<Weapon> weapons;
weapons.push_back(Weapon("Sword", 1, 2)); // if Weapon has such constructor
// or
weapons.push_back(create_weapon("Sword", 1, 2)); // a function that returns a Weapon

如果Weapon没有空构造函数:

std::vector<std::shared_ptr<Weapon>> weapons;
weapons.push_back(std::make_shared<Weapon>("Sword", 1, 2, 3));
// ^^^ similar to calling new Weapon("Sword", 1, 2, 3);

答案 3 :(得分:-1)

假设Weapon是一个类或结构,你可以这样做:

vector<Weapon> weapons;//create a vector of weapon objects
Weapon wp;
wp.createWeapon("Sword",1,2,3);
weapons.push_back(wp);//add the object to the vector