Copys of different pointer types to a single object

Hi, I wanted to have an array of base class pointers to access all the derived classes' member functions. Here is what I've typed

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
#include <iostream>
using namespace std;

class Base {
public:
	Base() { };
	virtual void print() { cout<<"Base\n"; };
};

class A : public Base {
public:
	A() { };
	void print() { cout<<"A\n"; };
	void funInA() { };
};

class B : public Base {
public:
	B() { };
	void print() { cout<<"B\n"; };
	void funInB() { };
};

int main() {
	Base* arrayOfBasePointers[10] = {};
	for (int i = 0; i < 5; i++) {
		arrayOfBasePointers[i] = new A;
		arrayOfBasePointers[i+5] = new B;
	} //[0]..[4] is of type A, [5]..[9] is of type B

	for (int i = 0; i < 10; i++) {
		arrayOfBasePointers[i]->print();
	} // this outputs nicely, A A A A A B B B B B

//	arrayOfBasePointers[3]->funInA(); //error
//	arrayOfBasePointers[8]->funInB(); //error
	// Question! How do you make it works?

	// I have tried this
	((A*)arrayOfBasePointers[3])->funInA(); //OK
	((A*)arrayOfBasePointers[8])->funInA(); //NOT supposes to be OK

	// and this
	A* arrayOfAObjects[5] = {};
	B* arrayOfBObjects[5] = {};
	for (int i = 0; i < 5; i++) {
		arrayOfAObjects[i] = (A*)arrayOfBasePointers[i];
		arrayOfBObjects[i] = (B*)arrayOfBasePointers[i+5];
	}
	arrayOfAObjects[3]->funInA();	//OK
	arrayOfBObjects[3]->funInB();	//OK

	//but then I cannot have a good feature like
	//for (int i = 0; i < 10; i++) {
	//	arrayOfBasePointers[i]->print();
	//}
}


At the end of the day, I ended up having both arrayOfAObjects and arrayOfBasePointers pointing to the same object. If I delete either one, the other one will have memory access violation, which I think is not a good way to achieve my goal.

Please share your opinion, thank you!
Last edited on
Have you considered adding another virtual function to your Base class?
virtual void fun() = 0;

This will make Base an abstract base class, requiring subclasses (A and B) to implement their own version of fun().

1
2
3
4
5
6
7
8
9
10
11
12
13
class A : public Base {
public:
    A() {}
    void print() { cout << "A\n"; }
    void fun() { cout << "fun in A\n"; }
};

class B : public Base {
public:
    B() {}
    void print() { cout << "B\n"; }
    void fun() { cout << "fun in B\n"; }
};


Now your loop need not be concerned with the specifics of the type.

1
2
3
4
...
    for (int i = 0; i < 10; ++i) {
        arrayOfBasePointers[i]->fun();
    }
Yes thank you. This will be a quick fix but in a long run, class A or B may have new functions being implemented while the Base class is not always available for amendment (i.e. adding in new virtual functions).
I believe this indicates a failure in your design pattern.

You should not be accessing derived class functions not present in the base class.
I'm in agreement with Duoas; however, if you just want to see if it can be done, look into dynamic_cast. http://cplusplus.com/doc/tutorial/typecasting/
Topic archived. No new replies allowed.