 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
xions@gmx.de Guest
|
Posted: Wed Feb 08, 2006 7:10 pm Post subject: Sharing a vector of object between two classes. |
|
|
Hi,
I am interessted in a elegant way to share one vector of objects
between two classes. The first class creates resp. fills the vector and
the second class updates it. The first class just creates/fills it
initially and has afterwards just reads from it. Write access is
reserved for the second class. How can I share this vector/list ? Has
any one a fancy solution ? My solution was a pointer. But the code
looks ugly:
vector<Device*>* deviceList
Maybe someone knows a better solution.
Thanks,
Marc
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Feb 09, 2006 10:06 am Post subject: Re: Sharing a vector of object between two classes. |
|
|
xions (AT) gmx (DOT) de wrote:
| Quote: | I am interessted in a elegant way to share one vector of objects
between two classes. The first class creates resp. fills the vector and
the second class updates it. The first class just creates/fills it
initially and has afterwards just reads from it. Write access is
reserved for the second class. How can I share this vector/list ? Has
any one a fancy solution ? My solution was a pointer. But the code
looks ugly:
vector<Device*>* deviceList
Maybe someone knows a better solution.
|
Depending on whether you want that data member change during the lifetime
of the two objects, the pointer can be replaced with an instance and
a reference, or an instance and a pointer.
class OwnsCreatesFillsReads {
vector<Device*> deviceVector;
...
};
class Updates {
vector<Device*> *pdeviceVector;
...
};
If 'Updates' object retains the association with the collection of the
Device pointers for its lifetime (and can never have it 'not set'), then
I'd recommend a reference here:
class Updates {
vector<Device*> &deviceVector;
...
};
Of course, just like with any other reference, it has to be initialised
during construction of the 'Updates' object and cannot be "reseated".
V
--
Please remove capital As from my address when replying by mail
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Thu Feb 09, 2006 11:06 am Post subject: Re: Sharing a vector of object between two classes. |
|
|
xions (AT) gmx (DOT) de wrote:
| Quote: | I am interessted in a elegant way to share one vector of objects
between two classes. The first class creates resp. fills the vector and
the second class updates it.
|
I'm not sure if you aren't confusing classes and instances of classes, but
I'll assume you really mean classes.
| Quote: | The first class just creates/fills it
initially and has afterwards just reads from it. Write access is
reserved for the second class. How can I share this vector/list ? Has
any one a fancy solution ? My solution was a pointer. But the code
looks ugly:
vector<Device*>* deviceList
|
You could move the vector to a common baseclass of both classes, that way
instances of both classes would have access to it.
If you means instances sharing a vector, you could use a reference instead
of a pointer. If both objects have different lifetimes, you should use a
shared_ptr instead.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Axter Guest
|
Posted: Thu Feb 09, 2006 11:06 am Post subject: Re: Sharing a vector of object between two classes. |
|
|
xions (AT) gmx (DOT) de wrote:
| Quote: | Hi,
I am interessted in a elegant way to share one vector of objects
between two classes. The first class creates resp. fills the vector and
the second class updates it. The first class just creates/fills it
initially and has afterwards just reads from it. Write access is
reserved for the second class. How can I share this vector/list ? Has
any one a fancy solution ? My solution was a pointer. But the code
looks ugly:
vector<Device*>* deviceList
Maybe someone knows a better solution.
|
You should avoid using pointers as much as possible.
When possible, prefer using reference over pointers.
You can design one class to have write access to the vector only on the
constructor, and then all the members have only read access. This can
easily be done by using a constant reference member variable.
Example code:
class WriteOnceReadMany
{
public:
WriteOnceReadMany(vector<Device> &deviceList):m_deviceList(deviceList)
{
//In constructor, you have access to populate the
//vector by using the input argument directly
}
void SomeFunction()
{
//Here you only have access to read the vector
//through the m_deviceList, because it's constant
}
const vector<Device> &m_deviceList;
};
class WriteAlways
{
public:
WriteAlways(vector<Device> &deviceList):m_deviceList(deviceList)
{
}
vector<Device> &m_deviceList;
};
int main(int argc, char* argv[])
{
vector<Device> My_deviceList;
WriteOnceReadMany My_WriteOnceReadMany(My_deviceList);
WriteAlways My_WriteAlways(My_deviceList);
If you really need to use a pointer inside your vector, than consider
using boost::shared_ptr, or one of following smart pointers:
http://code.axter.com/cow_ptr.h
http://code.axter.com/copy_ptr.h
http://code.axter.com/smart_ptr.h
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|