Assigning derived class of new-array failes

Hello all,

i am currently in the process of implementing a console version of greenfoot. I have assigned the base class (Entity) to each object on the 2-dimensional world with "new", but I want to be able to overwrite all these objects with a derived class of Entity, so that different objects can appear on the map.

when i do this currently, only every second object is populated with Empty, the others with the base class:
1
2
3
4
5
6
7
8
9
10
11
12
_____________________
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
| |w| |w| |w| |w| |w|
_____________________


my lib code is:

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

using namespace std;




class Entity {
public:
    char interpretation = 0;
    bool walkable = true;

};



class Empty: public Entity {
public:
    char interpretation = 'w';
    bool walkable = true;

};











class KaraWorld {

    void _preset(size_t, size_t);

    size_t _rows{};
    size_t _columns{};
    Entity **_map = (Entity **) new Entity*[_rows];


public:

    KaraWorld(): _map{(Entity **) new Entity*[10]}, _rows{10}, _columns{10} { _preset(10, 10); };
    KaraWorld(size_t row_s, size_t column_s): _map{(Entity **) new Entity*[row_s]}, _rows{row_s}, _columns{column_s}  { _preset(row_s, column_s); }
    KaraWorld(size_t row_s): _map{(Entity **) new Entity*[row_s]}, _rows{row_s}, _columns{row_s}  { _preset(row_s, row_s); }
    KaraWorld(const KaraWorld& cp): _map{cp._map}, _columns{cp._columns}, _rows{cp._rows} {}
    KaraWorld(KaraWorld&& other) {std::swap(_map, other._map); std::swap(_rows, other._rows); std::swap(_columns, other._columns);}
    ~KaraWorld() {for (size_t i = 0; i < _rows; i++) {delete[] _map[i];} delete[] _map;}


    const size_t rows() const {return _rows;}
    const size_t columns() const {return _columns;}

    Entity **map() {return _map; }

};

void KaraWorld::_preset(size_t r, size_t c) {
    for (size_t i = 0; i < r; i++) {
        _map[i] = new Empty[c]{};

    }

}








ostream& operator<<(ostream& os, KaraWorld& k) {
    for (size_t m = 0; m < k.columns() * 2 + 1; m++)
        os << "_";
    os << "\n";
    for (size_t i = 0; i < k.rows(); i++) {

        for (size_t j = 0; j < k.columns(); j++) {

            os << "|";
            os << k.map()[i][j].interpretation << ((j + 1) == k.columns() ? "|\n":"");

        }

    }
    for (size_t m = 0; m < k.columns() * 2 + 1; m++)
        os << "_";
    os << "\n";

    return os;

}


my testing code is:

1
2
3
4
5
6
7
8
#include <iostream>
#include "myKara.hpp"
using namespace std;

int main() {
    KaraWorld kara{10, 10};
    cout << kara;
}


thanks in advance for your effort,

Luke
Last edited on
I have assigned the base class (Entity) to each object on the 2-dimensional world with "new", but I want to be able to overwrite all these objects with a derived class of Entity

Instead of:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Entity {
public:
    char interpretation = 0;
    bool walkable = true;

};

class Empty: public Entity {
public:
    char interpretation = 'w';
    bool walkable = true;

};


you probably want to override the values in Entity. One way is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Entity {
public:
    Entity(char interpretation = 0, bool walkable = true) :
        interpretation(interpretation),
        walkable(walkable) {
    }
    char interpretation;
    bool walkable;
};

class Empty: public Entity {
public:
    Empty() : Entity('w', true) {
    }
};
Last edited on
Class [tt]Entity[tt] should have a virtual destructor.

Deleting an object through pointer to base invokes undefined behavior unless the destructor in the base class is virtual:
...
A common guideline is that a destructor for a base class must be either public and virtual or protected and nonvirtual.
https://en.cppreference.com/w/cpp/language/destructor#Virtual_destructors
Topic archived. No new replies allowed.