Cast a pointer to array to base class pointer to array

Discussion in 'C++' started by Hansen, Apr 19, 2010.

  1. Hansen

    Hansen Guest

    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
    Hansen, Apr 19, 2010
    #1
    1. Advertising

  2. Hansen wrote:

    > 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 **.
    Michael Tsang, Apr 19, 2010
    #2
    1. Advertising

  3. Hansen

    Puppet_Sock Guest

    On Apr 19, 5:04 am, "Hansen" <>
    wrote:
    > 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
    Puppet_Sock, Apr 19, 2010
    #3
  4. Hansen

    rep_movsd Guest

    On Apr 19, 5:46 pm, Puppet_Sock <> wrote:
    > On Apr 19, 5:04 am, "Hansen" <>
    > wrote:
    >
    > > 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."


    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.

    >
    > 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
    rep_movsd, Apr 24, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    695
    Ivan Vecerina
    Oct 12, 2003
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    531
    John Carson
    Sep 3, 2005
  3. Alberto Luaces
    Replies:
    6
    Views:
    376
    Fraser Ross
    Jun 19, 2006
  4. Replies:
    1
    Views:
    328
  5. , India
    Replies:
    8
    Views:
    932
    gwowen
    Aug 18, 2010
Loading...

Share This Page