• Advertisement

Archived

This topic is now archived and is closed to further replies.

collection

This topic is 5805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two classes, A and B, which are both based on the abstract base class C. Now I want to write a collection which I can add and remove elements of types A and B to. Then I''m going to loop through the collection and run one of C''s pure virtual functions that is defined in both A and B. I''m not sure if this is possible. How do you suggest me to do?

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Well... of course this is perfectly possible

you have something like

class C
{
public:
virtual void func(void) = 0;
}

class A : public class C
{

}

class B : public class C
{

}

now... the only thing you have to do is make an array of class type C object... for example

class A a;
class B b;
class C *p_list[2];

then do something like

p_list[0] = &a
p_list[1] = &b

(or overload constructors... but that is a bit more difficult THOUGH NOT IMPOSSIBLE)

now when you do

p_list[0]->func();

will call class a''s func

p_list[1]->func();

will call class b''s func

now... there is one other trick to remember... make sure that you declare the destructors of class A & B virtual... so that calling classes of type C will destruct using A or B destructors as well.

Well... I hope this was of any help
Greetz, Dark


ICQ: 130925152
Email: e.j.folkertsma@student.utwente.nl

Share this post


Link to post
Share on other sites
You can''t store types A and B in a single collection, you will have to store pointer to type C, which will also give you polymorphic behaviour.

  
typedef C* elem;
typedef std::vector<elem> container;
container v_c;

// add elements


for( container::iterator it = v_c.begin();it != v_c.end(); ++it )
(*it)->doSomething();


Don''t forget to delete the container elements when you''ve finished with them, which will require a virtual dtor in C.


Share this post


Link to post
Share on other sites
It works like a charm! But your collection class must contain pointers to the base class C. And then each element is added by first allocating a new instance of the desired class:
C *pA = new A;
or
C *pB = new B;
and then:
Collection.Add(pA);
or
Collection.Add(pB);

Here is some source that uses the STL vector template as the collection class.

  
#include <stdio.h>
#include <vector>
using std::vector;

class C
{
public:
virtual void Function() = 0;
};

class A : public C
{
public:
void Function();
}

class B : public C
{
public:
void Function();
}

void A::Function()
{
printf("A''s function\n");
}

void B::Function()
{
printf("B''s function\n");
}

void main()
{
vector< C * > Collection;

Collection.push_back( new A );
Collection.push_back( new A );
Collection.push_back( new B );

for(int i = 0; i < Collection.size(); i++)
Collection[i]->Function();
}


Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Don't forget to delete the container elements when you've finished with them, which will require a virtual dtor in C.



Good explenation, but this last one is false, making it virtual is enough. Having it pure virtual serves no other purpose than making clients yell at you and calling you stupid because they end up creating empty dtors all over the place.

[EDIT]NOTE: I WAS HIGH WHEN I WROTE THIS, the quote didn't read the same here as it did in my deranged mind.. the point about not making a dtor pure virtual still aplies tho.

Edited by - DigitalDelusion on February 26, 2002 12:05:39 PM

Share this post


Link to post
Share on other sites
quote:
Original post by DigitalDelusion
Good explenation, but this last one is false, making it virtual is enough. Having it pure virtual serves no other purpose than making clients yell at you and calling you stupid because they end up creating empty dtors all over the place.



Well, it''s a good job that you only imagined I said "make it pure virtual" then, isn''t it?


Share this post


Link to post
Share on other sites

  • Advertisement