Hi all,
Consider the following code fragment:
// some data structure
class Data { ... }
// Container for the data structure
Class Container
{
public:
Container(const & Data data): m_cRefData(data ), m_ncRefData(dat a)
{}
private:
const Data& m_cRefData;
Data& m_ncRefData;
};
// some func to create the data
Data getData(int i, int j, string k, const char* l)
{
return Data(i,j,k,l);
}
// some func to process the container
void processContaine r(Container& container)
{
// work with container.mcRef Data and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l ) );
// .... do some other things ... amount of time taken may varies ...
processContaine r(container);
My question is what happen if I used the data member of the container?
Will the value initialized in the Container constructor still valid?
I read something about temporaries bind to const& have a lifetime of
the const&. If that holds, container.mncRe fData may not be valid by
the time it is process but, container.mcRef Data is still valid by the
time it is processed. Is that assumption correct?
Thanks,
Kaede
Consider the following code fragment:
// some data structure
class Data { ... }
// Container for the data structure
Class Container
{
public:
Container(const & Data data): m_cRefData(data ), m_ncRefData(dat a)
{}
private:
const Data& m_cRefData;
Data& m_ncRefData;
};
// some func to create the data
Data getData(int i, int j, string k, const char* l)
{
return Data(i,j,k,l);
}
// some func to process the container
void processContaine r(Container& container)
{
// work with container.mcRef Data and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l ) );
// .... do some other things ... amount of time taken may varies ...
processContaine r(container);
My question is what happen if I used the data member of the container?
Will the value initialized in the Container constructor still valid?
I read something about temporaries bind to const& have a lifetime of
the const&. If that holds, container.mncRe fData may not be valid by
the time it is process but, container.mcRef Data is still valid by the
time it is processed. Is that assumption correct?
Thanks,
Kaede
Comment