Hello

Let me describe the problem: I've got 1 base class item which is inherited by a few other subclasses. Everything goes well until I put an object from a subclass into an array of items I lose the specific data of the subclass.

I think I need something of pointers but I really don't get it right. I've tried everything I can think of but it doesn't work.

Any ideas?

Thanks,

Jasper

Recommended Answers

All 3 Replies

You need downcasting(downcast your base class pointer to a pointer to your derived class -- go "down" the class hierarchy). See this for a good example (aside from the initial post).

First , make sure that your function is not overriding. In case of overriding,data can be override.
Second thing, check the initialized capacity of array,and it should not be less than the size of objects.
If error will not recover then post that code on this site, I will check it and solve the problem.
My name is Sunny Zode

It got solved. I had to cast it to the new type because it thinks it was an item pointer but actually it was something else (a subclass).

CItem* a[5];
	CPotion p2;
	CEquipment p;
	a[0] = &p;
	a[1] = &p2;
	CEquipment * p3 = (CEquipment *)a[0]; 
	CPotion * p4 = (CPotion *)a[1];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.