| | |
help:stl vector of user defined objects
![]() |
•
•
Join Date: Jan 2007
Posts: 3
Reputation:
Solved Threads: 0
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
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.
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);
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.
•
•
Join Date: Jan 2007
Posts: 3
Reputation:
Solved Threads: 0
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
I hope i made some sense.
thanks
kiran
•
•
•
•
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
¿umop apisdn upside down? •
•
Join Date: Jan 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
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??
C Syntax (Toggle Plain Text)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class container { //empty base class }; //////////////////////////////////////// template <typename T> class Matrix { ~Matrix(){ delete x; for (int i = 0; i<container.size(); ++i) delete container[i]; Matrix<T>& operator=( Matrix<T> & rhs) { container.push_back( new equals<T>(*this, rhs); //creates equals class object return *this; } Matrix<T>& operator* (Matrix<T>& b) { //First create the matrix for the output result Matrix<T> *c = new Matrix<T>; container.push_back( new product<T>(*this,b,*c); //creates product class object return *c; } protected: std::vector< container* > container; }; //derived class template <typename T> class equals : public sv_pipeOps { public: equals(Matrix<T>& a, Matrix<T> &b) { ......... } //This is the code that processes a pipeline data section. void Operator() { .......... } }; ////derived class template <typename T> class product : public sv_pipeOps { public: product(Matrix<T>& a, Matrix<T>& b, Matrix<T>& c) { .................. } //This is the code that processes a pipeline data section. void Operator() { .................................. } }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Last edited by ~s.o.s~; Jan 11th, 2007 at 12:33 pm. Reason: Added code tags, learn to use them.
![]() |
Similar Threads
- STL vector - deleting the last element (C++)
- User-defined functions (C++)
- User-Defined Function - part 2 (C++)
- error in user defined string class (C++)
- using(STL)function object (bind2nd) with a user defined function object (C++)
Other Threads in the C Forum
- Previous Thread: Simple string manipulation
- Next Thread: Solution to Build Error
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






