counting number of base and derived classes pointers

I have to write a function to count number of pointers to objects of base class and number of pointers to objects of derived class that are stored in array.
I already know how to do it using RTTI.
But I really do not have an idea how to do it outside the classes.
I would really appreciate some help.
That's how my classes and small piece of function look like:

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
116
117
118
119
120
121
122
class IntVector
{

    int* tab;
    int size;
    int* _useCount;


public:
    const int max = 7;

    IntVector(int s) : tab(new int[s]), size(s), _useCount(new int)
    {
        if (s <= 0 || s > max)
            throw Size(s);
        else
        {
            size = s;
            tab = new int;
        }
    }

    IntVector(const IntVector& src) : tab(src.tab), size(src.size), _useCount(src._useCount)
    {
        (*_useCount)++;
    }

    class Range
    {
        int index;
    public:
        int getIndex() { return index; };
        Range(int i) : index(i) { }
    };

    class Size
    {
        int id;
    public:
        int getIndex() { return id; };
        Size(int i) : id(i) { }
    };



    int& operator[](int i)
    {
        if (i >= 0 && i < size)
            return tab[i];
        else
            throw Range(i);
    }

    virtual ~IntVector()
    {
        if (--(*_useCount) == 0)
        {
            delete[] tab;
            delete _useCount;
        }
    }
};

class StringIntVector : IntVector
{
    int* _useCount;
    int* tab;
    int size;

public:

    StringIntVector(int s) : IntVector(s), tab(new int[s]), size(s), _useCount(new int)
    {
        if (s <= 0 || s > max)
        {
            throw Size(s);
        }
        else
        {
            size = s;
            tab = new int;
        }
    }

    StringIntVector(const StringIntVector& src) : IntVector(src), tab(src.tab), size(src.size), _useCount(src._useCount)
    {
        (*_useCount)++;
    }

    int& operator[](string s)
    {
        int s_s = s.length();
        if (s_s < size)
            return tab[s_s];
        else
            throw Range(s_s);
    }
};

bool count(IntVector* source[], int n, int& base, int& derived)
{
    IntVector* vec();

    for (int i = 0; i < n; i++)
    {

        if (???)
        {
            base++;
        }
        else
        {
            derived++;
        }
    }

    if ((base + derived) == n)
        return true;
    else
        return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Foo{
public:
   virtual ~Foo() = default;
};

class Bar: public Foo{
};


int count_bar(Foo * array[], int size){
   int result = 0;
   for(int K=0; K<size; ++K)
      if(dynamic_cast<Bar*>(array[K]))
         ++result;
   return result;
}
Last edited on
Topic archived. No new replies allowed.