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

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);

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

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.

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??

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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() 
    {
      ..................................
    }

};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.