dont know how to call...

I have a class Items(base class) with class weapons : public Items.

now I have called
vector<Items*> Item;

and created a Item with
Item.emplace_back(new weapon);

now im trying to get a Variable from a weapon
protected:
int weapondamage;

when I had it just as I Item I would get info by
Item[x]->getVariable();

but that does not work with this now... any help would be great.
Last edited on
An Items can never be a Weapons, however a pointer (or reference) to the Items type can refer to an object of Weapons type.

In your code, Item.emplace_back(new weapon) results in a memory leak (if it even compiles.) Item does not hold pointers.
sorry the vector<Items*> Item should be the line, I missed the *
It would help if you supplied a minimal, compilable example. The few snippets you show don't quite get across what you're trying to do and how you're trying to do it.
ok well I am using this code. the way I under stand things is that u have your base class and the variables that (Items) all the other classes use. Then u have the other classes which I have weapon (which is a item) and ill have armor(which is a item) and maybe later (potions ) but with the weapons class I put in that it has a private: int basedamage, mindamage.
because armor and potions don't have damage.
some help would be greatly appreciated and try to solve my question first and if there is other things u see a suggestion would be good as I have only been at this for about 2 weeks with learning it and trying to have some fun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
void equipItem(vector<Items*> &Item, Player* play, string input, string Inventory[])
{
	cout << "Now in equip\n";
	// check item in inventory 
	string str = input;
	str.erase(0,6);					// erase 'equip '
	unsigned int itemIsThere = -1;
	if (str.size() > 3)				// make sure there is at least 4 letters to compare against inventory
	{
		// = itemInInventory(Inventory, str);
		//string completeName = Inventory[itemIsThere];
		int number = itemInventoryVectorNum(Item, str);
		if (number != -1)
		{
			cout << Item[number]->getIType() << endl;
			if (Item[number]->getIType() == "weapon")
			{
				cout << Item[number]->getISlot()<< endl;
				if (Item[number]->getISlot() == "righthand")
				{
					if (play->getRightHandFree())
					{
						//need to equip item
						play->setRighthand(Item[number]->getIName());
						play->setRightHandFree(false);
						Item[number]->setILoc(EQUIPPED);
						removeFromInventory(Inventory, Item[number]->getIName());
						//add bonuses to character
						play->setWBaseDam(weapon::wBaseDamage()[number]-); // I created weapons and if im understanding the way things work then I guess things could be way outta wack
						play->setWMinDam(1);
						return;
					}
					else
					{
						cout << "There is a " << play->getRighthand() << " equipped. Would you like to swap items? y/n?\n";
						bool answer = getYesNo();
						{
							if (answer)
							{
								cout << "Lets swap that item\n";
								//remove item from equip
								play->setRighthand("");
								//play->setWBaseDam();
								//remove bonuses
								//add new item to equip
								//add bonuses
								return;
							}else cout << "Left " << play->getRighthand() << " equipped.\n"; return;
						}
					}
				}



weapon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "items.h"
class weapon :
	public Items
{
private:
	int wBaseDamage, wMinDamage;

public:
	
	weapon(void);
	virtual ~weapon(void);
	void WAttack();
	void setBaseDam(int base);
	void setMinDam(int min);
	int getBaseDam();
	int getMinDam();
};

items.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
#include<string>
class Items
{
protected:
	int iLoc;
	std::string iName, iArea, iSlot, iType;

public:
	Items(void);
	Items(std::string n, std::string a, int loc);
	~Items(void);
	void setISlot(std::string slot);
	void setILoc(int l);
	void setIName(std::string name);
	void setIArea(std::string area);
	void setIType(std::string type);
	int getILoc();
	std::string getIName();
	std::string getIArea();
	std::string getISlot();
	std::string getIType();
};

Your code is neither minimal, nor compilable, but I can see where you're coming from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

struct Item
{
    Item(const std::string& name) : _name(name) {}
    std::string name() const { return _name; }

private:
    std::string _name;
};

struct Weapon : public Item
{
    Weapon(const std::string& name, int damage) : Item(name), _damage(damage) {}

    void setDamage(int dam) { _damage = dam; }
    int getDamage() const { return _damage; }

private:

    int _damage;
};

int main()
{
    Weapon weapon("Axe", 6);

    Item* iptr = &weapon;

    // Your question seems to be:  I have a pointer to Item, how do I
    // access the get or setDamage methods of Weapon?
    // Well, if we KNOW that iptr points to a Weapon object, we can
    // use a static cast.
    static_cast<Weapon*>(iptr)->setDamage(10);
}


However, it isn't very likely that you will know in generic code what the derived type of the pointer you have is pointing to. So, we'll add a virtual destructor to the Item class to make the Item class and any class that derives from it polymorphic, then we may use a dynamic cast to determine the type of the object pointed to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

struct Item
{
    Item(const std::string& name) : _name(name) {}
    std::string getName() const { return _name; }

    virtual ~Item() {}

private:
    std::string _name;
};

struct Weapon : public Item
{
    Weapon(const std::string& name, int damage) : Item(name), _damage(damage) {}

    void setDamage(int dam) { _damage = dam; }
    int getDamage() const { return _damage; }

private:

    int _damage;
};

struct Special : public Item
{
    Special(const std::string& name, const std::string& otherProperty)
        : Item(name), _other(otherProperty) {}

    std::string getOther() const { return _other; }

private:
    std::string _other;
};

int main()
{
    std::srand(std::time(0));

    for (unsigned i = 0; i < 10; ++i)
    {
        Item* iptr;
        switch (std::rand() % 3)
        {
        case 0: iptr = new Weapon("Axe", 6); break;
        case 1: iptr = new Special("Deodorant", "Feminine"); break;
        case 2: iptr = new Item("Generic"); break;
        default: std::cout << "Unexpected value encountered\n"; return 0;
        }

        // how do we know what we've got here?  Is it a Special?  A Weapon?  Just a generic Item?
        if (Weapon* w = dynamic_cast<Weapon*>(iptr))
        {
            std::cout << "We have a weapon with name: " << w->getName();
            std::cout << "\nand damage: " << w->getDamage() << '\n';
        }
        else if (Special* s = dynamic_cast<Special*>(iptr))
        {
            std::cout << "We have a special with name: " << s->getName();
            std::cout << "\nand property: " << s->getOther() << '\n';
        }
        else
            std::cout << "We have an item with name: " << iptr->getName() << '\n';

        delete iptr ;
    }
}


http://ideone.com/sJJTKe
http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast
Last edited on
Topic archived. No new replies allowed.