Re: Passing derived class object array in place of base class object array

From: Tom Widmer (tom_usenet_at_hotmail.com)
Date: 12/02/04


Date: Thu, 02 Dec 2004 10:45:06 +0000

On 2 Dec 2004 00:51:28 -0800, justanotherguy63@yahoo.com wrote:

>Hi,
>
>I am designing an application where to preserve the hierachy and for
>code substitability, I need to pass an array of derived class object in
>place of an array of base class object. Since I am using vector
>class(STL), the compiler does not allow me to do this.

I think with "business" entities, the kind of class that has a base
class, you would typically store a vector of pointers. e.g.
std::vector<Derived*> or std::vector<shared_ptr<Derived> >. Usually
such entities aren't copyable, so can't be placed in containers
directly. In any case, this level of indirection is useful.

>I do realize there is a pitfall in this approach(size of arrays not
>matching etc), but I wonder how to get around this problem. I have a
>class hierachy with abstract base class and all I wonder why the
>principle of passing a derived class reference in place of base class
>is not extended to arrays. After all, we are making the code more
>substitutable right? How do we achieve substitutability in this case?
>To me at least, not having to make needless changes to code and having
>the ability to sort things out dynamically using inheritance has always
>sounded crisp.

If you have a std::vector<Derived*>, you can create a vector<Base*>
simply with:
std::vector<Base*>(v.begin(), v.end());
and pass that. It will only involve a single memory allocation (even
in the smart pointer case), which isn't bad.

It efficiency is a problem, then you do have an alternative that
should work for single, non-virtual inheritence (where the address of
the derived object usually matches that of the base one). It is
undefined behaviour though, so only use it if profiling identifies a
problem:

std::vector<Base*> const& vb =
  reinterpret_cast<std::vector<Base*>&>(vd);

>Also, does this problem mean there is some deficiency in my application
>design? Any light you throw on this will be greatly appreciated.

Well, you might find that you can get away with having a
std::vector<Base*> all the time anyway, and do away with the
vector<Derived*>.

Tom



Relevant Pages

  • Re: Returning an unknown number of types/values
    ... but it's an array which contains a fundamental type of which I ... The derived class ... can be done in the base class). ... and have the base's accessor throw ...
    (alt.comp.lang.learn.c-cpp)
  • Passing derived class object array in place of base class object array
    ... code substitability, I need to pass an array of derived class object in ... place of an array of base class object. ... principle of passing a derived class reference in place of base class ...
    (comp.lang.cpp)
  • Re: A postincrement operator
    ... derived class object. ... global function there is always a problem how to prevent using a base ... Why do you want a constructor for a derived class from a base class in the first place? ...
    (microsoft.public.vc.language)
  • Re: Another question about inheritance (up-casting and down-casting)
    ... so even if you type cast a derived class object to a base class object ... > public void finalize(){ ...
    (comp.lang.java.programmer)
  • Re: A postincrement operator
    ... functions usually have parameters as const reference. ... derived class object. ... Why do you want a constructor for a derived class from a base class in the ...
    (microsoft.public.vc.language)