Jason Heyes wrote:[color=blue]
> "tuvok" <520001085531-0001@t-online.de> wrote in message
> news:d8gl6p$m5v$05$1@news.t-online.com...[color=green]
>>"Jason Heyes" <jasonheyes@optusnet.com.au> wrote[color=darkred]
>>>"Larry I Smith" <larryXiXsmith@verizon.net> wrote in message
>>>>Artie Gold wrote:
>>>>>Larry I Smith wrote:
>>>>>>tuvok wrote:
>>>>>>
>>>>>>>Can these objects be stored in the same collection class (for
>>>>>>>example
>>>>>>>in a vector or map)?
>>>>>>>
>>>>>>IF they all derive from the same base class (e.g. circle, rectangle,
>>>>>>triangle all derive from shape), then you can store them as
>>>>>>objects of the base class (e.g. as 'shape'). Otherwise - no.
>>>>>>
>>>>>Erm...
>>>>>
>>>>>You can store *pointers* to them (but not the objects themselves) as
>>>>>*pointers to the base class* in this case; of course in that case,
>>>>>you'd
>>>>>have to manage the objects' lifetimes manually.
>>>>>
>>>>>HTH,
>>>>>--ag
>>>>Yes, you are correct. Base class pointers only...
>>>>
>>>Write a class to encapsulate the base class and hide the management of
>>>object lifetime using boost::shared_ptr. Objects (not pointers to
>>>objects)
>>>of the base-encapsulating class can be stored in a std::vector like any
>>>other object.[/color]
>>Will then the right virtual functions be invoked?
>>[/color]
>
> Absolutely. For example, say Animal is your base and Animal has a virtual
> function called make_noise. Here is the encapsulating class (called a
> handle) for the Animal base class:
>
> class AnimalHandle
> {
> boost::shared_ptr<Animal> animal_ptr;
>
> public:
> AnimalHandle() : animal_ptr(new Animal()) { }
>
> void make_noise() const { animal_ptr->make_noise(); }
> };
>
> You can store AnimalHandle objects in std::vector. You don't need to store
> pointers to AnimalHandle.
>
>[/color]
You should advise the OP that Boost is not part of the C++ Standard.
It's an add-on that will have to be obtained/installed.
http://www.boost.org/
Larry