wrote:
> I have a function in a class:
> void A::aFunction (B& b) {
> // do something
> ...
> }
>
> void A::anotherFunction() {
> // need a reference of B again.
>
> }
>
> my question is how can I create an attriubte of A which can hold B
> after A::aFunction() is called?
Do you really need it? What if the B object is destroyed after
A::aFunction
returns? What if aFunction is called twice? Which B is needed then?
You might be able to hold a copy of B. If it's designed properly, it
will have
a copy constructor if and only if you can copy it. Of course, that
means that
anotherFunction will work on a copy of b, but at least A can ensure the
lifetime of that copy.
A proper design would probably involve a smart pointer. Either
std::auto_ptr<B>,
boost::shared_ptr<B> or std::tr1::shared_ptr<B> could work.
HTH,
Michiel Salters