For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2005 > std:list of pure virtual base classes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author std:list of pure virtual base classes
Corey Wirun

2005-03-03, 4:02 pm

Hi All,

I'm writing an STL-based port of the old Borland BIDS containers. I'm
struggling with how lists and list iterators are used in BIDS and how to
port this to STL so things work seemlessly.

Here's the class I'm trying to store in a std::list.

class CBase
{
CBase() {};
~CBase() {};

int DBRead() = 0;
};

'Course, I'm actually 'new'ing an instance of one of several derived
classes, but the std::list only knows
about CBase to genericise the storage.

A couple of BIDS template-type classes I'm trying wrap std::list and it's
iterator:

template <class T>
class TContainer: public std::list<T>
{
...
};

template <class T>
class TContainerIterator
{
public:
TContainer<T>::iterator iter;
...
};

Now the problem: BIDS allows for something like this:

typedef TContainer<CBase> TContainerMine;
typedef TContainerIterator<CBase> TIteratorMine;

But, I cannot create a new TContainerMine because CBase is an abstract class
and it cannot create it. My next step would be to change the typedefs to
<CBase *>, but that'll mean changing all the code that uses the templates.
I was hoping to just change the template class defs and the calling code
would still work as is.

I realize this is kind of a bizarre scenario, but I'm stuck.

Thanks in advance!
Corey.


Pete Becker

2005-03-03, 9:00 pm

Corey Wirun wrote:
>
> Now the problem: BIDS allows for something like this:
>
> typedef TContainer<CBase> TContainerMine;
> typedef TContainerIterator<CBase> TIteratorMine;
>
> But, I cannot create a new TContainerMine because CBase is an abstract class
> and it cannot create it.


Right. I suspect what you used was a TIContainer, that is, an indirect
container that holds pointers; all the accessors dereference the
pointers. (Please don't ask BIDS questions. <g> I wrote that library,
but it was a long time ago, and I've forgotten most of what went into it)

> My next step would be to change the typedefs to
> <CBase *>, but that'll mean changing all the code that uses the templates.
> I was hoping to just change the template class defs and the calling code
> would still work as is.
>


Nevertheless, that's basically what you need to do. STL deals in value
types. Pointers are values, so you can create a list<CBase*>, but what
you get out of it are CBase*'s, not CBase&'s. BIDS did that indirection
for you, so you could easily get at the pointed-to objects using the
same syntax as for a container that held actual objects. STL doesn't do
that, so your wrapper classes will need to. And in this case, don't use
inheritance; you don't want the interface to list<CBase*>. Write your
own container with the interface you need, and implement it with a
list<CBase*>.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Carl Daniel [VC++ MVP]

2005-03-03, 9:00 pm

Pete Becker wrote:

> STL doesn't
> do that, so your wrapper classes will need to. And in this case,
> don't use inheritance; you don't want the interface to list<CBase*>.
> Write your own container with the interface you need, and implement it
> with a
> list<CBase*>.


A near-future version of Boost (www.boost.org), maybe as soon as 1.33, will
include the indirect container library: a collection of STL-like containers
that operate indirectly, like the BIDS TIContainer<T> class did.

-cd




Pete Becker

2005-03-03, 9:00 pm

Carl Daniel [VC++ MVP] wrote:
> Pete Becker wrote:
>
>
>
>
> A near-future version of Boost (www.boost.org), maybe as soon as 1.33, will
> include the indirect container library: a collection of STL-like containers
> that operate indirectly, like the BIDS TIContainer<T> class did.
>


Thanks for pointing that out. I saw a mention of the indirect container
library the other day, but didn't make the connection.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Corey Wirun

2005-03-03, 9:00 pm

Thanks Carl,

But Boost doesn't have an implementation of 'list' right now, does it? I'm
taking that to be that an indirect dbl-linked list will not be in the cards?

C.

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:%23CB%23d9CIFHA.3472@TK2MSFTNGP09.phx.gbl...
> Pete Becker wrote:
>
>
> A near-future version of Boost (www.boost.org), maybe as soon as 1.33,

will
> include the indirect container library: a collection of STL-like

containers
> that operate indirectly, like the BIDS TIContainer<T> class did.
>
> -cd
>
>
>
>



Carl Daniel [VC++ MVP]

2005-03-03, 9:00 pm

Corey Wirun wrote:
> Thanks Carl,
>
> But Boost doesn't have an implementation of 'list' right now, does
> it? I'm taking that to be that an indirect dbl-linked list will not
> be in the cards?


It doesn't now, but the proposed indirect containers library will include
one. It's named boost::ptr_list (last I knew). That library isn't included
in Boost yet, but it's been reviewed and accepted and is in the final stages
of preparation for release. I would expect it to be included in boost 1.33
(current is boost 1.32), but there's no accounting for unforeseen delays.

I you're CVS literate, you could get the latest (unreleased) Boost CVS
snapshot - I don't know if the indirect containers library is in there yet,
but I wouldn't be surprised.

-cd


Corey Wirun

2005-03-04, 8:59 pm

Carl,

Is 1.33 days/ws/months away. I also do freeware development, and know
what's it's like to commit to dates, but getting a ballpark indication of
1.33 would tell me if I should wait it out a little, or try and continue
writing my own.

Thanks in Advance!
Corey.


"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:O2h7sgDIFHA.904@tk2msftngp13.phx.gbl...
> Corey Wirun wrote:
>
> It doesn't now, but the proposed indirect containers library will include
> one. It's named boost::ptr_list (last I knew). That library isn't

included
> in Boost yet, but it's been reviewed and accepted and is in the final

stages
> of preparation for release. I would expect it to be included in boost

1.33
> (current is boost 1.32), but there's no accounting for unforeseen delays.
>
> I you're CVS literate, you could get the latest (unreleased) Boost CVS
> snapshot - I don't know if the indirect containers library is in there

yet,
> but I wouldn't be surprised.
>
> -cd
>
>



Carl Daniel [VC++ MVP]

2005-03-04, 8:59 pm

Corey Wirun wrote:
> Carl,
>
> Is 1.33 days/ws/months away. I also do freeware development, and
> know what's it's like to commit to dates, but getting a ballpark
> indication of
> 1.33 would tell me if I should wait it out a little, or try and
> continue writing my own.


I'd guess it's at least a couple months away, since 1.32 is still fairly
new. I don't recall seeing any discussion on the boost mailing list about a
1.33 release date yet.

In the meantime you can probably get the indirect containers pre-release
library from the boost CVS or there may be a tarball/ZIP file with the
library. The way to find out would be to subscribe to the boost-developers
mailing list and ask. See www.boost.org for subscription info.

-cd


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com