Cast a pointer to array to base class pointer to array

H

Hansen

Hi,

A have a pointer to an array of a derived class, which I want to cast to its
base class. That is, im trying to do something like this:
void f( derivedClass* derivedData[] )
{
baseClass* data[] = dynamic_cast<baseClass*[]>(derivedData);
}

This can't compile, so I'm wondering how to make it work?

Best Regards
Hansen
 
M

Michael Tsang

Hansen said:
Hi,

A have a pointer to an array of a derived class, which I want to cast to
its base class. That is, im trying to do something like this:
void f( derivedClass* derivedData[] )
{
baseClass* data[] = dynamic_cast<baseClass*[]>(derivedData);
}

This can't compile, so I'm wondering how to make it work?

Best Regards
Hansen

This does not work. You are casting a 2-D pointer into an array of pointers,
which certainly doesn't work. Also, you are initialising an array, which is
not possible.

Casting a Derived ** to a Base ** is unsafe, similar to casting a Foo **
into a const Foo **.
 
P

Puppet_Sock

Hi,

A have a pointer to an array of a derived class, which I want to cast to its
base class. That is, im trying to do something like this:
void f( derivedClass* derivedData[] )
{
    baseClass* data[] = dynamic_cast<baseClass*[]>(derivedData);

}

This can't compile, so I'm wondering how to make it work?

You can't cast collection of a derived type to collection of
a base type. Otherwise you'd have some guy coming to park his
helicopter in the spot intended for motorcycles.

Yes, when you need a vehicle, you can use either a motorcylce
or a nuclear submarine. But it does not make sense to try to
store a vehicle. You need a specific type of vehicle, because
you need to know how big it is, even if it has exactly the
same interface. So you can't have an array of "vehicle" and
then fill it with motorcyles. Nor can you have an array of
motorcycles and fill it with "vehicles."

You need to refactor a bit. You need to find a way to get
where you want to without trying to collect abstract vehicles.

Just two examples: You could store pointers to vehicles
instead of actual vehicles. You could look at some kind of
pointer-to-implementation scheme. There are lots of other
possibles, but what you choose depends on the specifics
of your application.
Socks
 
R

rep_movsd

A have a pointer to an array of a derived class, which I want to cast to its
base class. That is, im trying to do something like this:
void f( derivedClass* derivedData[] )
{
    baseClass* data[] = dynamic_cast<baseClass*[]>(derivedData);

This can't compile, so I'm wondering how to make it work?

You can't cast collection of a derived type to collection of
a base type. Otherwise you'd have some guy coming to park his
helicopter in the spot intended for motorcycles.

Yes, when you need a vehicle, you can use either a motorcylce
or a nuclear submarine. But it does not make sense to try to
store a vehicle. You need a specific type of vehicle, because
you need to know how big it is, even if it has exactly the
same interface. So you can't have an array of "vehicle" and
then fill it with motorcyles. Nor can you have an array of
motorcycles and fill it with "vehicles."

It's not quite the case here :
The OP is not "putting vehicles into an array of motorcycles"
More like "putting vehicle pointers in an array of motorcycle
pointers."

AFAIK all pointers are created equal and nothing stops you from
treating an array of pointers to one type as an array of pointers to
another.

The real question here is why one would want to do such a thing in the
first place - One legitimate use I can think of is :

Let say you had an array of vehicle pointers and a function that adds
another N vehicles to that set

#define MAX_VEHICLE 1000
typedef Vehicle* PVehicle;
PVehicle *vehicles[MAX_VEHICLE];
int nVehicles = 0;

bool AddVehicles(PVehicle toAdd, int count)
{
if(count + nVehicles > MAX_VEHICLE) return false;
for(int i = 0; i < count; ++i)
vehicles[nVehicles++] = toAdd;

return true;
}


void someFn()
{
Car *pCars[20];
// Initialize pCars somehow with 20 cars

PVehicle *pVeh = (PVehicle *)pCars;
AddVehicles(pCars, 20);
}

This is quite reasonable, even though the design is bad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top