Dynamically created objects seem useless, don't understand.

Hello.

So in order to create a C++ object on the heap, you need a pointer on the stack. To me, it then seems useless, because you have to have declared the pointer statically in the program. I must be missing something.

If I am not clear on what I am confused about, I can clarify further.
Say you have a program where you have a few different type of objects that can be created. And you want any number of these objects to be created.

And, at compile time you don't know how many of which objects are going to be created. Say, for example, the user can control when to create which object.

You cannot accomplish that without dynamic allocation.
Also, if you're going to use polymorphism, you going to need a pointer.
Say, for example, that in a game where you build a village, you have the class House. It is up to the user to create Houses over time. Say you have a function with a pointer in it to create an instance of the House class. Once you change the pointer to create a new House, the old House is no longer accessible,
which makes it useless when you cannot interact with oldHouse by using a function. I can't figure out a solution to this.
Once you change the pointer to create a new House, the old House is no longer accessible

You need to save the pointer to the old house somewhere. There are a couple of ways to deal with this.

1) Assuming that houses are part of a village, have a village object that contains a list (or vector) of houses in the village.

2) Have a object that represents your world. Within that world object, maintain a list (or vector) of all the objects (including houses) in your world.

Do you think I could just have a vector of Houses without the Village object, and just add the Houses right after I create the new House, use the pointer to copy it to the vector. Once I do that, I should be able to just use the vector, and delete the original one that was created, right?
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
// http://ideone.com/gfgPH7
#include <iostream>
#include <string>

// We have a house that has an address
struct House
{
    std::string address;
};

// And a region that contains houses
struct Region
{
    House* houses = nullptr;
    unsigned num_houses = 0;
};

// This is how we might add a house to a region:
void add_house(Region& region, const std::string& address)
{
    House* new_houses = new House[region.num_houses + 1];

    for (unsigned i = 0; i < region.num_houses; ++i)
        new_houses[i] = region.houses[i];

    new_houses[region.num_houses].address = address;

    delete [] region.houses;

    region.houses = new_houses;
    ++region.num_houses;
}

void show_region(const Region& region)
{
    std::cout << "This region contains houses at the following addresses:\n";
    for (unsigned i = 0; i < region.num_houses; ++i)
    {
        std::cout << '\t' << region.houses[i].address << '\n';
    }
}

int main()
{
    Region region;
    
    std::cout << "Enter addresses to add houses to a region (or an empty line to stop):\n";
    std::string line;
    while (std::cout << "> ", std::getline(std::cin, line) && !line.empty())
        add_house(region, line);

    show_region(region);

    delete [] region.houses ; // normally this would be done in a destructor.
}


Of course, using a container class which manages the memory for you should be preferred:
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
http://ideone.com/bfWyvC
#include <iostream>
#include <string>
#include <vector>

// We have a house that has an address
struct House
{
    std::string address;
};

// And a region that contains houses
struct Region
{
    std::vector<House> houses;
};

// This is how we might add a house to a region:
void add_house(Region& region, const std::string& address)
{
    region.houses.emplace_back(House{address});
}

void show_region(const Region& region)
{
    std::cout << "This region contains houses at the following addresses:\n";
    for (auto& house : region.houses)
        std::cout << '\t' << house.address << '\n';
}

int main()
{
    Region region;
    
    std::cout << "Enter addresses to add houses to a region (or an empty line to stop):\n";
    std::string line;
    while (std::cout << "> ", std::getline(std::cin, line) && !line.empty())
        add_house(region, line);

    show_region(region);
}


Thank you cire!
Topic archived. No new replies allowed.