By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

read-only access to a data member

P: n/a
Hi,

What's the "best" way to have an object ObjA of class A have read-access
(but not write-access) to a data member of object ObjB of class B. Class B
has a std::vector<T> as a datamember. ObjA should be able to get elements of
that vector and, if possible, a read-only (const?) reference to the vector
as such. Is it possible to do this without a classical ObjB.Get() member
function? Since, for the element-wise access, this extra indirection level
could be non-negligable; I think....

bye,
gert
Nov 3 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
gvdeynde wrote:
Hi,

What's the "best" way to have an object ObjA of class A have read-access
(but not write-access) to a data member of object ObjB of class B. Class B
has a std::vector<T> as a datamember. ObjA should be able to get elements of
that vector and, if possible, a read-only (const?) reference to the vector
as such. Is it possible to do this without a classical ObjB.Get() member
function? Since, for the element-wise access, this extra indirection level
could be non-negligable; I think....


You could give B a container interface, but with only const member
functions:

class B
{
public:
typedef std::vector<T> V;
typedef V::const_iterator const_iterator;

const_iterator begin() const
{
return v_.begin();
}

const_iterator end() const
{
return v_.end();
}

private:
V v_;
};
Jonathan

Nov 3 '05 #2

P: n/a
Jonathan's suggestion of const_iterators is good. As an aside, the
"classical get method" is perfectly fine too: after all, your vector
look ups run operator[] which is a fancy-named get method. As long as
it's defined inline, there's no cost after optimisation. Tony

Nov 3 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.