GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jan-2007, 09:54
mpakala mpakala is offline
New Member
 
Join Date: Jan 2007
Posts: 2
mpakala is on a distinguished road

help with stl vector of non-homogenous derived objects


hi

I need to fill a stl vector with different derived class objects.Thats
what I want to acheive with my implementation. Hence I'm using
polymorphism so that I can create a vector of base class pointers and
then use that vector to create new objects of derived classes.
Please let me know if there are any issues with my implementation.


CPP / C++ / C Code:
//base class
class container {
virtual ~container(){ };
virtual Operator() = 0; //this is overridden in derived classes
}

//derived class
template <typename T> class product : public sv_pipeOps {

public:
product(Matrix<T>& a, Matrix<T>& b, Matrix<T>& c)
{
//implementation
}

//virtual function Operator overridden here
void Operator()
{
//implementation
}

};

//derived class
template <typename T> class equals : public container {

public:
equals(Matrix<T>& a, Matrix<T>& b)
{
//implementation
}

//virtual function Operator overridden here
void Operator()
{
//implementation
}

};


template <typename T> class Matrix
{
~Matrix(){
delete x;
for (int i=0; i<container_bptr.size(); ++i) delete container_bptr[i];


Matrix<T>& operator*( Matrix<T> & rhs)
{
 Matrix<T> *c = new Matrix<T>;
 container_bptr.push_back( new product<T>(*this,b,*c); //creates product class object
 return *c;
}

Matrix<T>& operator=( Matrix<T> & rhs)
{
container_bptr.push_back( new equals<T>(*this, rhs); //creates equals class object
return *this;
}

protected:
std::vector< container* > container_bptr;

};

regards
kiran
Last edited by LuciWiz : 13-Jan-2007 at 06:02. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 11-Jan-2007, 21:32
davis
 
Posts: n/a

Re: help with stl vector of non-homogenous derived objects


They're not "non-homogenous" (the word you're looking for is heterogenous) if they all derive from something else so that they share a common base. Otherwise, there is no practical way to store heterogenous objects in a vector. When we initialize a vector, we specify the typename of the type that it will store. It isn't as if we're likely to get away from that "protocol" any time soon.


:davis:
  #3  
Old 12-Jan-2007, 08:51
mpakala mpakala is offline
New Member
 
Join Date: Jan 2007
Posts: 2
mpakala is on a distinguished road

Re: help with stl vector of non-homogenous derived objects


hi

yes..you are right...actually iam new to stl stuff so i did not know how to frame the question...i just wanted to emphasize that the objects that are referred by the vector of base class pointers are different(heterogenous)...
but does the implementation looks correct to you ??
thanks
kiran
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
References to base class to access derived class only members mirizar C++ Forum 3 03-Dec-2005 20:40
controling dialog objects & multiple source files omills MS Visual C++ / MFC Forum 0 14-Jul-2004 23:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.