Array of derived classes

Hello.
I have 2 classes, one is the base (bibliorafia) ad the other the derived class (sinedria).
I want to create an array of pointers that start as the type of the base and then change it to the type of the derived. What i mean is the following code:
1
2
3
4
bibliografia *test;//bibliografia is the base class and sinedria is the derived
test = new sinedria[2];
test[0].probolh();//probolh() is a function to display the contents of the class
test[1].probolh();


This compiles perfect but every time I run it it has a problem going to test[1].probolh()...The program stops responding before entering the probolh() function because the windows prevent its execution as probable harming. Which, as i understand, means that my program is trying to read a memory location outside of the program.
I added some cout's to see the process and i found out that when I enter the data to the derived class (through the constructor) they are correct. But the problem occurs when i try to call it and retrieve the data. Then all the values are incorect (like random) and they cause an array to go out of bounds.


If i don't use array (test = new sinedria)or if the pointer is from the beggining only as the derived type then it works fine, but I want to do it as the base class because after that i will make more derived classes and i want to have them all in a 2D array.

Do you know if there is any way to do something like that?

Thank you in advance.
Last edited on
One choice is to use the pointer, not the object.
like this:
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
#include <iostream>
using namespace std;

class bibliografia
{
public:
	bibliografia() {}
	virtual void probolh()
	{
		cout << "from biliografia..." << endl;
	}
};

class sinedria : public bibliografia
{
public:
	sinedria(){size = 0;}
	virtual void probolh()
	{
		cout << "from sinedria..." << endl;
	}
private:
	int size;
};

int main()
{
	bibliografia **test;
	test = new bibliografia*[2];
	test[0] = new sinedria();
	test[1] = new sinedria();
	test[0]->probolh();
	test[1]->probolh();
	return 0;
}
Yes, i can do that and store each derived class in its aray. But is there any way to do the same thing with different derived classes? For example it 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
#include <iostream>
using namespace std;

class bibliografia
{
public:
	bibliografia() {}
	virtual void probolh()
	{
		cout << "from biliografia..." << endl;
	}
};

class sinedria : public bibliografia
{
public:
	sinedria(){size = 0;}
	virtual void probolh()
	{
		cout << "from sinedria..." << endl;
	}
private:
	int size;
};

class anotherone: public bibliografia
{
public:
	anotherone(){size = 0;}
	virtual void probolh()
	{
		cout << "from anotherone..." << endl;
	}
private:
	int size;
};
int main()
{
	bibliografia ***test;
	test = new bibliografia**[2];
         test[0] = new bibliografia*[1];
         test[1] = new bbliografia*[1];
	test[0][0] = new sinedria;
	test[1][0] = new anotherone();
	test[0][0]->probolh();
	test[1][0]->probolh();
	return 0;
}


I did this. But still i have the same problem as before...
Last edited on
Do you mean that you want to have an array of classes that do not share a common ancestor?
No no, they all have the same ancestor (bibliografia).
the code you paste works perfect in my computer....
except the wrong "bbliografia" in line 42...
what problem you met?
can you paste it?
It worked perfect for me also...
I don't know, probably i made a logic error and made it to miss data...

Thank you for you help.
Topic archived. No new replies allowed.