944,128 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5876
  • C RSS
Jan 9th, 2007
0

help:stl vector of user defined objects ??

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mpakala is offline Offline
3 posts
since Jan 2007
Jan 9th, 2007
0

Re: help:stl vector of user defined objects

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);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Jan 9th, 2007
0

Re: help:stl vector of user defined objects

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mpakala is offline Offline
3 posts
since Jan 2007
Jan 9th, 2007
0

Re: help:stl vector of user defined objects

Click to Expand / Collapse  Quote originally posted by mpakala ...
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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jan 10th, 2007
0

Re: help:stl vector of user defined objects

Click to Expand / Collapse  Quote originally posted by Bench ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mpakala is offline Offline
3 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Simple string manipulation
Next Thread in C Forum Timeline: Solution to Build Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC