Objects holding Objects

Hello there, I'm steadilly progressing through some C++ Begineers textbooks, quite near to the end of them now, and I'm having a bit of difficulty with an object linking an object.

I've posted a sample code, which is just a watered down version of my larger project, but it is just the one error, I know the problem, but I'm unsure of how to go about it.

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
#include <iostream>
#include <string>

using namespace std;

class Armour
{
public:
       Armour(int strength);
       int GetStrength() const;
       
private:
        int m_Strength;
};

Armour::Armour(int strength)
{
       m_Strength = strength;
}

int Armour::GetStrength() const
{
    return m_Strength;
}


class Dwarf
{
public:
       Dwarf(int strength);
       int GetStrength() const;
       
       void SetEquip(Armour& equip);

       
private:
        int m_Strength;
        Armour* chest;
};

Dwarf::Dwarf(int strength)
{
    m_Strength = strength + chest->GetStrength();
}

int Dwarf::GetStrength() const
{
    return m_Strength;
}

void Dwarf::SetEquip(Armour& equip)
{
     chest = equip;
}


int main()
{
    Dwarf dwarfOne(10);
    Armour goldHauberk(10);
    
    cin.get();
    cin.ignore();
    
    return 0;
}


The SetEquip function, I realise I am handling the pointer in the wrong way.

cannot convert `Armour' to `Armour*' in assignment


Could someone explain the correct usage of what I am trying to achieve.

I believe all the information someone with more experience needs is all there, however feel free to ask for further details, and of course, thank you in advance! :)
Line 53 should be:

chest = &equip;

chest is a pointer, equip is a reference. You need to assign equip's address to chest, instead of the object itself.
The right syntax would be

1
2
void Dwarf::SetEquip(Armour& equip )
    {   chest = &equip; }


however this design is a bit dangerous because Dwarf cannot be guaranteed that
the pointer is ever valid. For example, this would cause problems:

1
2
3
4
5
6
7
8
9
10
11
Dwarf* MakeDwarf() {
    Armour a( 5 );
    Dwarf* d = new Dwarf( 3 );
    d->SetEquip( a );
    return d;
}

int main() {
    Dwarf* d = MakeDwarf();
    std::cout << "Dwarf's armour strength is " << d->GetEquip()->GetStrength() << std::endl;
}


Assuming you had a GetEquip() method on Dwarf, the GetStrength() would crash because chest
points to a variable that was allocated on the stack inside MakeDwarf().
This thread has a few bits of information that you might find helpful going forward. The current design will grow linearly a member pointer and accessors for each additional type of item. I'm betting that after you have Armor, Weapon, Potion, etc. members you'll notice the value in storing them all (or at least groups of items) in containers.
http://www.cplusplus.com/forum/general/25798/
however this design is a bit dangerous because Dwarf cannot be guaranteed that
the pointer is ever valid.


What would be a better approach to this? I seem to have improved the code so that I can indeed link the two, however as you suggested this leads to crashes or an incorrect value.
Topic archived. No new replies allowed.