Rolf Magnus wrote:
[color=blue]
> Søren Holstebroe wrote:[color=green]
>> This doesn't work:
>> ------------------
>> template <typename T, template <typename ELEM> class CONT = std::vector>
>> class pointainer : public CONT<T *>
>> {
>> public:
>> void foo() {
>> CONT<T *>::iterator it;
>> }
>> };[/color]
>
> It isn't supposed to. Try:
>
> typename CONT<T *>::iterator it;[/color]
This works. Thank you.
[color=blue][color=green]
>> Currently I only see the solution of using composition instead of
>> inheritance which should clean any semantic problems, but will throw out
>> my free lunch.[/color]
>
> What would you gain by that? You still couldn't use it in a context where
> the base class is needed. If you just want to prevent anyone from trying
> this, you could inherit privately and get the member functions you want to
> keep into your derived class with using declarations.[/color]
My purpose was to semantically prevent base usage so private inheritance is
a solution. I just could get my using statements right and was in doubt
that this was the right track to follow anyhow. My overall purpose is to
make hide all the ugly memory management while still being able to use
containers of base class pointers with simple semantics. Ie. I want my
programs to be as readable, semantically simple and maintainable as my Java
programs.
The following naïve pointainer works with g++:
----------
#include <vector>
#include <iostream>
template <typename T, template <typename ELEM> class CONT = std::vector>
class pointainer : CONT<T *>
{
public:
using CONT<T *>::iterator;
using CONT<T *>::const_iterator;
using CONT<T *>::push_back;
using CONT<T *>::begin;
using CONT<T *>::end;
~pointainer() {
for (typename CONT<T *>::const_iterator it=begin();it!=end();it++) {
delete *it;
}
}
};
using namespace std;
class A {
public:
int v;
A(int _v) : v(_v) {cout << "A::ctor"<<endl;}
~A() {cout << "A::~dtor"<<endl;}
};
int main() {
pointainer<A> p;
p.push_back(new A(5));
p.push_back(new A(42));
for (pointainer<A>::const_iterator it=p.begin();it!=p.end();it++) {
cout << (*it)->v << endl;
}
return 0;
}
----------
Output is as expected:
A::ctor
A::ctor
5
42
A::~dtor
A::~dtor
--------------
Søren Holstebroe -
http://www.holstebroe.dk