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
- Next message: Peter Koch Larsen: "Re: Is this good style of C++?"
- Previous message: Ioannis Vranos: "Re: Is this good style of C++?"
- In reply to: justanotherguy63_at_yahoo.com: "Passing derived class object array in place of base class object array"
- Next in thread: Dietmar Kuehl: "Re: Passing derived class object array in place of base class object array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Peter Koch Larsen: "Re: Is this good style of C++?"
- Previous message: Ioannis Vranos: "Re: Is this good style of C++?"
- In reply to: justanotherguy63_at_yahoo.com: "Passing derived class object array in place of base class object array"
- Next in thread: Dietmar Kuehl: "Re: Passing derived class object array in place of base class object array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|