help:stl vector of user defined objects

Reply

Join Date: Jan 2007
Posts: 3
Reputation: mpakala is an unknown quantity at this point 
Solved Threads: 0
mpakala mpakala is offline Offline
Newbie Poster

help:stl vector of user defined objects ??

 
0
  #1
Jan 9th, 2007
Hi

Im trying to use the stl vector to implement a container. The container doesnot need any data and member functions. It should just act as a stack for the objects.
But the objects are not homogneous.so I don t think i can use vector. How can I implement this ?
eg:

vector<container*> ops;

ops = new A(.....); A is a user defined class
ops = new B(.....);B is a user defined class

I m not sure how to write the class container. What does it need to be a valid type to be used with stl vector?
class container{
container(){};
~container(){};
??????
}

Please let me know.
thanks
kiran
Last edited by mpakala; Jan 9th, 2007 at 6:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help:stl vector of user defined objects

 
0
  #2
Jan 9th, 2007
If class A and B are your classes, I would create class C to be the base class of A and B, then

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


vector<C*> ops;

A* a = new A(.....); A is a user defined class
ops.push_back(a);

B* = new B(.....);B is a user defined class
ops.push_back(b);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3
Reputation: mpakala is an unknown quantity at this point 
Solved Threads: 0
mpakala mpakala is offline Offline
Newbie Poster

Re: help:stl vector of user defined objects

 
0
  #3
Jan 9th, 2007
thanks a lot for your response. I was about edit my post. Actually I have vector of container class pointers ( eg: vector<container* > ops; ) . So I guess I can make those pointers to point to non-homogenous objects. All Iam saying is that since vector stl requires the objects to be homogeneous I will have container class pointers as the type for vector and then I would use them to create non-homogenous user-defined class objects. Also I have trouble figuring out how to build the class container ??
I hope i made some sense.
thanks
kiran
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: help:stl vector of user defined objects

 
0
  #4
Jan 9th, 2007
Originally Posted by mpakala View Post
thanks a lot for your response. I was about edit my post. Actually I have vector of container class pointers ( eg: vector<container* > ops; ) . So I guess I can make those pointers to point to non-homogenous objects. All Iam saying is that since vector stl requires the objects to be homogeneous I will have container class pointers as the type for vector and then I would use them to create non-homogenous user-defined class objects. Also I have trouble figuring out how to build the class container ??
I hope i made some sense.
thanks
kiran
The vector isn't actually storing the objects, its only storing pointers to them - as long as your 'container' class (The base class, or interface class) has some virtual functions, you can use polymorphism to access the overridden functions in the derived class objects.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3
Reputation: mpakala is an unknown quantity at this point 
Solved Threads: 0
mpakala mpakala is offline Offline
Newbie Poster

Re: help:stl vector of user defined objects

 
0
  #5
Jan 10th, 2007
Originally Posted by Bench View Post
The vector isn't actually storing the objects, its only storing pointers to them - as long as your 'container' class (The base class, or interface class) has some virtual functions, you can use polymorphism to access the overridden functions in the derived class objects.
Hi
thanks for your response. I dont need to access any derived class stuff in my implementation. I just need to store a bunch of classes as a vector of some base class container. so this implementation worked but please let me know if there are any issues with this. I just used a empty class called container. Is it ok to do that??
  1. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. class container
  3. {
  4. //empty base class
  5. };
  6.  
  7. ////////////////////////////////////////
  8. template <typename T> class Matrix
  9. {
  10. ~Matrix(){
  11. delete x;
  12. for (int i = 0; i<container.size(); ++i) delete container[i];
  13. Matrix<T>& operator=( Matrix<T> & rhs)
  14. {
  15. container.push_back( new equals<T>(*this, rhs); //creates equals class object
  16. return *this;
  17. }
  18.  
  19. Matrix<T>& operator* (Matrix<T>& b)
  20. {
  21. //First create the matrix for the output result
  22. Matrix<T> *c = new Matrix<T>;
  23. container.push_back( new product<T>(*this,b,*c); //creates product class object
  24. return *c;
  25. }
  26.  
  27. protected:
  28.  
  29. std::vector< container* > container;
  30.  
  31. };
  32.  
  33. //derived class
  34. template <typename T> class equals : public sv_pipeOps {
  35.  
  36. public:
  37.  
  38. equals(Matrix<T>& a, Matrix<T> &b)
  39. {
  40. .........
  41. }
  42.  
  43. //This is the code that processes a pipeline data section.
  44. void Operator()
  45. {
  46. ..........
  47. }
  48. };
  49.  
  50.  
  51. ////derived class
  52. template <typename T> class product : public sv_pipeOps {
  53.  
  54. public:
  55.  
  56. product(Matrix<T>& a, Matrix<T>& b, Matrix<T>& c)
  57.  
  58. {
  59. ..................
  60. }
  61.  
  62. //This is the code that processes a pipeline data section.
  63. void Operator()
  64. {
  65. ..................................
  66. }
  67.  
  68. };
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Last edited by ~s.o.s~; Jan 11th, 2007 at 12:33 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC