Teen Programmers Unite  
 

 

Return to forum top

c++: set and inheritance

Posted by ltanuwij [send private reply] at September 08, 2001, 05:46:15 AM

her is my question;
I have a class called A and this class has two childs; class B and C,
in the main()I create a 'set'(STL) of class A;
set<A> one;
set<A> ::iterator iter;
my questioon is Can I add a new object of class B or C to the set, if yes how?
I know it can be done by using the array of object.

thank you

Posted by drdevil [send private reply] at September 08, 2001, 11:10:09 AM

You would have ta have a 'resizable' array. So, for a simplistic one.
malloc() memory for the size of the current array plus one more object, then copy the old one to the new memory location, and add extra object, then change array ptr to new memory location..

Gaz

Posted by ltanuwij [send private reply] at September 08, 2001, 11:24:21 AM

but can I use the set (STL) instead of the array?

Posted by lordaerom [send private reply] at September 08, 2001, 01:41:52 PM

I wouldn't think there would be any problems, but from your posting, would it be well advised to assume you've tried and failed?
I don't see why it should be a problem, as a B 'is an' A. If you have tried and get an error, what is it?

Posted by Psion [send private reply] at September 08, 2001, 02:32:46 PM

It is positively silly to think that it would allow subclasses! Haha! Laugh at Lordaerom!

This isn't Java. The STL containers store members by value, not by reference. Make a Set of A*'s and you're fine.

Posted by lordaerom [send private reply] at September 08, 2001, 10:31:31 PM

Why is that positively silly?
If B is a subclass of A, you can treat it as an A. If there is a function insert (const A& val) (Such as, oh, the one in the set class), I can give it a B, and that is OK!

---
#include <iostream>
#include <set>
using namespace std;

class A
{
private:
int i;
public:
void setI(int a)
{
i = a;
}

int getI() const
{
return i;
}

bool operator< (const A &val) const
{
return val.getI() < i;
}
};

class B : public A
{
};
class C : public A
{
};

void main()
{
A a;
a.setI(12);
B b;
b.setI(512);
C c;
set<A> s;
set<A>::iterator iter;

s.insert(a);
s.insert(b);
s.insert(c);

iter = s.begin();
while(iter != s.end()) {
cout << (*iter).getI() << endl;
iter++;
}
}

It's not quite a good way to do it, since some information is lost it seems, but it's does accept the values.

Posted by gian [send private reply] at September 08, 2001, 07:40:35 PM

Everyone laugh at Gian... because you lost him quite a number of posts ago....

Posted by ltanuwij [send private reply] at September 09, 2001, 05:38:31 AM

first, I want to say thank you to Lordaerom, now I have a question about the above code, here is Lordarom's code that I modified (I use the constructor);
#include <iostream>
#include <set>
using namespace std;

class A
{
private:
int i;
public:

A (int a)
{
i=a;
}
void getIn()
{
cout<<i;
}
virtual void getI() = 0;

bool operator< (const A &val) const
{
return val.getI() < i;
}
};

class B : public A
{
public:
int f;
B(int d, int e) :
A(d)
{ f=e;}
void getI()
{
A::getIn();
cout<<f;
}
};

void main()
{
B b (512, 12);
set<A> s;
set<A>::iterator iter;

s.insert(b);

iter = s.begin();
while(iter != s.end()) {
cout << (*iter).getI() << endl;
iter++;
}
}
and now i have a problem of getting the content of 'f' variable using the pure virtual function. sorry for all the trouble.

thank you

Posted by Psion [send private reply] at September 09, 2001, 05:15:29 PM

This is what I was talking about. Lordaerom's example uses the same data members for the subclasses. If you add additional ones, they will not be stored in the container. Forget Lordaerom's Fountain of Youth promise and just use pointers like I said if you want to do this sort of thing. :-)

Posted by yontihy [send private reply] at October 09, 2001, 03:01:16 AM

store only pointers :)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.