K
kaede
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(data)
{}
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 processContainer(Container& container)
{
// work with container.mcRefData and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l) );
// .... do some other things ... amount of time taken may varies ...
processContainer(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.mncRefData may not be valid by
the time it is process but, container.mcRefData 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(data)
{}
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 processContainer(Container& container)
{
// work with container.mcRefData and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l) );
// .... do some other things ... amount of time taken may varies ...
processContainer(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.mncRefData may not be valid by
the time it is process but, container.mcRefData is still valid by the
time it is processed. Is that assumption correct?
Thanks,
Kaede