"Mark Probert" <> wrote in message
news:QZZ6e.986608$8l.977913@pd7tw1no...
> Hi ..
>
> A basic template and pointer question for you.
>
> I have a set of classes that look like (simplified):
>
> class Foo {
> public:
> Foo *next;
> Foo *other;
> int n;
>
> friend class FooList;
> };
>
> class FooList {
> vector<Foo> foos;
Your later usage implies that you meant:
std::vecotr<Foo*> foos.
> void add(); // add a foo to the list
> };
>
> void FooList::add() {
> Foo f = new Foo();
> f->n = foos.size();
>
> Foo *fp = foos.end(); // get the last added foo to link the pointers
You get a reference to the last item using
Foo* fp = foos.back();
> if (fp != (Foo*)NULL)
> fp->next = &f;
> foos.push_back(f);
> }
>
> My problem is that the 'Foo *fp = foos.end();' is illegal.
> How go I get the last item as it is stored?
While my intuition says that there may be better approaches to accomplishing
your final goal, the following will do what you were explicitly asking for.
class Foo
{
Foo* next;
Foo* other;
int n;
public:
Foo():next(0),other(0),n(0){}
Foo( std::vector<Foo*>& foos );
void AttachNext( Foo* aNext ){ next = aNext; }
...
};
class FooList
{
std::vector<Foo*> foos;
public:
void add(){ foos.push_back( new Foo( foos ) ); }
};
Foo::Foo( std::vector<Foo*>& foos ):next(0),other(0),n(foos.size())
{
if( !foos.empty() ) foos.back().AttachNext( this );
}
Jeff Flinn
|