Array Initializer and a generic cast mechanism

Perhaps I am just missing something obvious but I need to construct and initialize an array of objects in a class that manages those objects.

My code to construct the objects compiles fine. The problem is when I try initializing the objects. It stems from the fact that the template functionality has no way to know (that I know of) that the template type being passed will always be derived from a particular base class. Here is the code that won't compile. This segment is contained in a loop that goes through and initializes all the objects with the necessary data; this loop is located in a template function.

1
2
3
4
plot = &map->plots()[x][y];
IPlotLinkedItem* item = dynamic_cast<IPlotLinkedItem*(mItemCollection[counter]);
if (item)
	item->setPlot(plot);


The compiler complains that I cannot cast the template type as the specific type of cast I am requesting (I am assuming because it is unaware of there relationship). Is there a way to attempt to generically cast the item at run time and have it be null if the casting doesn't work? (I thought that was how dynamic_cast worked... apparently not).

EDIT:
One more piece of usefull information. The mItemCollection is cast as an array of QGraphicsItem because that is the base class and the IPlotLinkedItem is just an interface that the resulting object also inherits from.

I suppose I could make IPlotLinkedItem inherit QGraphicsItem but that complicates things. I would rather just use a generic method of casting dynamically.
Last edited on
The compiler complains that I cannot cast the template type as the specific type of cast I am requesting (I am assuming because it is unaware of there relationship).
No, that's not right. The compiler can see the compile-time types of all the objects involved because they're deducible from the point of use of the template. If the compiler is complaining that the dynamic_cast is not possible, that means you're casting between unrelated types. E.g. dynamic_cast<Iguana *>(mammal) is a cast between unrelated types, because no Mammal * can ever point to an Iguana.
If you're getting this kind of error, either there's a serious design flaw in your code, or you're trying to fetch a given datum from the wrong part of the object.

The mItemCollection is cast as an array of QGraphicsItem because that is the base class and the IPlotLinkedItem is just an interface that the resulting object also inherits from.
So you're saying that the compile-time type of the pointer is A, the run-time type of the object is B, and B is a subclass of both A and C, where C is not a subclass of A, and you're trying to perform this cast:
1
2
A *a = /*...*/;
C *c = dynamic_cast<C *>(a);
That's a cast between unrelated types. dynamic_cast<T1 *>(T2) can only cast pointers where there is an inheritance path from T2 (base) to T1 (derived).
This is like having a pointer to an Equine and trying to cast it to a pointer to a Bird, because you happen to know that the particular object is a Pegasus. dynamic_cast will never let you do that.
Last edited on
That all makes sense. Unfortunately, I am at a loss on how to accomplish what I need to then. I have created a new thread to explain the issue. It is located here. http://www.cplusplus.com/forum/general/193610/
Topic archived. No new replies allowed.