Function to create object

I have declaring instances of my equipment class everytime I need to use it. Is there a way I can use a function to do it so I don't have to remember everytime what the specifics of that object were and use the function to create the object then use it in a different function? something like this?

void player::createSword()
{
sword("Sword", 2, 0);
}


my current code... line 63 and 71, 89, 97
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include "equipment.h"
#include "player.h" 




player::player()
{
                setArmor(0);
                setAttackPoints(1);
                
}

void player::setArmor(int newArmor)
{
     armorPoints = newArmor;
}

int player::getArmor()
{
    return armorPoints;
}

void player::setAttackPoints(int newAttack)
{
     attackPoints = newAttack;
}

int player::getAttack()
{
    return attackPoints;
}
     
void player::displayInventory()
{
     if ( inventory.empty() )
     {
          std::cout << "You have nothing in your inventory." << std::endl;
     }
     else
     {
         std::cout << "You have " << inventory.size() << " items in your inventory." << std::endl;
         
         for ( int x = 0; x < inventory.size(); x++ )
         {
             std::cout << inventory[x].itemName;
             if ( inventory[x].itemName == weaponEquipped )
             {
                  std::cout << "   [Equipped]" << std::endl;
             }
             else
             {
                 std::cout << std::endl;
             }
         }     
     }
}

void player::equipSword()
{
     equipment sword("Sword", 2, 0);
     setAttackPoints(sword.getDamage());
     weaponSlot = true;
     weaponEquipped = "Sword";
}
   
void player::addSword()
{    
     equipment sword("Sword", 2, 0);
     inventory.push_back(sword);
     
}

void player::removeSword()
{
     for ( int x = 0; x < inventory.size(); x++ )
     {
         if ( inventory[x].itemName == "Sword" )
         {
              inventory.erase(inventory.begin() + x);
         }
     }
}

void player::equipAxe()
{
     equipment axe("Axe", 4, 0);
     setAttackPoints(axe.getDamage());
     weaponSlot = true;
     weaponEquipped = "Axe";
}

void player::addAxe()
{
     equipment axe("Axe", 4, 0);
     inventory.push_back(axe);
}

void player::removeAxe()
{
     for ( int x = 0; x < inventory.size(); x++ )
     {
         if ( inventory[x].itemName == "Axe" )
         {
              inventory.erase(inventory.begin() + x);
         }
     }
}
     



Last edited on
No you cannot for normal commands such as cin>> or cout<<, so i am pretty sure that in this case it will not work either
I know I can't use it for cin and cout, I just want to create one function for creating the object, so I don't have to retype it everytime. I'm not sure if that makes sense or is possible.

Besides that, does my code look okay?
Do you mean something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...

sword* player::createSword()
{
    return new sword("Sword", 2, 0);
}

// ...

int main()
{
    player player1( ... );
    sword* a_sword = player1.createSword();

    some_func( a_sword );

    delete a_sword; // <-- do not forget this
}


Or a more safer way is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <memory>
// ...

std::unique_ptr<sword> player::createSword()
{
    return std::unique_ptr<sword>( new sword("Sword", 2, 0) );
}

// ...

int main()
{
    player player1( ... );
    auto a_sword = player1.createSword(); // a_sword's type is unique_ptr

    some_func( a_sword );

   //delete a_sword; // <-- not needed, the destructor of unique_ptr<> automatically frees memory allocated by new
}
Last edited on
Topic archived. No new replies allowed.