Creating an array of pointers to base class to point to derived class objects dynamicaly

Please consider the following code :
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
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

class superclass
{
public:
    unsigned int a;
    superclass(){a=1;}
    virtual ~superclass(){}
};
class subclass1 : public superclass
{
public :
   unsigned int b;
   subclass1(){b=2;}
   ~subclass1(){}
};
class subclass2 : public superclass
{
    public :
   unsigned int c;
   subclass2(){c=3;}
   ~subclass2(){}
};

class master
{
public:
    superclass **superman;
    master()
    {
        superman=new superclass*[2];
        subclass1 s1;
        s1.b=8;
        *superman[0]=(superclass)s1;
        subclass2 s2;
        s2.c=9;
        *superman[1]=(superclass)s2;
    }
    ~master()
    {
        delete[]superman;
    }
};
int main()
{
    try{
    master m;
    cout<<"1"<<endl;
    subclass1 *s1=dynamic_cast<subclass1*>(m.superman[0]);
    subclass2 *s2=dynamic_cast<subclass2*>(m.superman[1]);
    cout<<"2"<<endl;
    
    cout<<"hello "<<" "<<" "<<s1->b<<endl;
    }catch(exception e){e.what();}
    return 0;
}


As you can see I want to create a dynamicaly alocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.I am sure I am missing something small, any help would be great.

PS: I could not find a similar question, but if it exists, please redirect me, also i would like a solution that does not require me to use vector or any inbuilt pre-existing library class.
Last edited on
pointers to a parent class can point to child classes - no casting needed (in fact this will throw away the child parts of the object)

(not sure why you have called this 'master' instead of just having a constructor - and destructor)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class master
{
public:
    superclass **superman;
    master()
    {
        superman=new superclass*[2];
        subclass1 *s1 = new subclass1;
        s1->b=8;
        superman[0] = s1;
        subclass2 *s2 = new subclass2;
        s2->c=9;
        superman[1] = s2;
    }
    ~master()
    {
        delete superman[0];
        delete superman[1]
        delete [] superman;
    }
};

Last edited on
Yes, this contains stuff you don't want to use. No, you aren't required to use those things, but it's convenient for me to do, since I don't want to reimplement them. If your compiler doesn't support C++11, you may have to do a little adjusting. The code you probably want to look at is in print

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
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <memory>


struct A
{
    int a ;
    A(int _a=1) : a(_a) {}
    virtual ~A() {}
};

struct B : public A
{
    int b ;
    B(int _a=1, int _b=2) : A(_a), b(_b) {}
};

struct C: public A
{
    int c ;
    C(int _a=1, int _c=3) : A(_a), c(_c) {}
};

typedef std::vector<std::unique_ptr<A>> container_type ;

void print(const container_type & container)
{
    for ( auto& ptr : container )
    {
        B* b = dynamic_cast<B*>(ptr.get()) ;
        if ( b )
            std::cout << "B: { a=" << b->a << ", b =" << b->b << " }\n" ;
        else
        {
            C* c = dynamic_cast<C*>(ptr.get()) ;
            if ( c )
                std::cout << "C: { a=" << c->a << ", c=" << c->c << " }\n" ;
            else                // it's an A.
                std::cout << "A: { a=" << ptr.get()->a << " }\n" ;
        }
    }
}

int main()
{
    std::srand(std::time(0)) ;

    container_type v ;

    for ( unsigned i = 0 ; i < 10 ; ++i )
    {
        switch( rand() % 3 )
        {
        case 0: v.emplace_back(new A) ; break ;
        case 1: v.emplace_back(new B) ; break ;
        case 2: v.emplace_back(new C) ; break ;
        }
    }

    print(v) ;
}

Topic archived. No new replies allowed.