Hello,
My question concerns as to how a pointer is passed by reference as a
function argument. The following is from code taken from the MICO
implementation of the CORBA specification.
in include files:
typedef Context *Context_ptr;
typedef ObjOut<Context> Context_out;
the function implementation is:
void
CORBA::ORB::get_default_context (Context_out res)
{
res = new Context ("");
}
and the way I invoke the function is:
Context_ptr ctx;
orb->get_default_context(ctx);
So, what I believe it happens here is: I pass to the get_default_context()
function the pointer ctx. Because of the "res = new Context ("");" the
pointer should be passed by reference. So, I believe (?) the function
prototype is equivalent to something looking like
CORBA::ORB::get_default_context (Context*& res). However, I wonder if I can
get a detailed expailation as to how this is achieved and if there are any
runtime costs due to the actual implementation. Is it really equivalent to
writting CORBA::ORB::get_default_context (Context*& res) ? Is there any
constructor called when I pass the pointer ctx to the function?The ObjOut
definition is given below (not too sure if I included everything
necessary):
template<class T>
class ObjOut
{
private:
T*& _ptr;
public:
ObjOut (T*& p);
ObjOut (ObjVar<T>& p);
ObjOut (const ObjOut<T>& p) : _ptr (p._ptr) { }
ObjOut<T>& operator= (const ObjOut<T>& p) {
_ptr = p._ptr;
return *this;
}
ObjOut<T>& operator= (const ObjVar<T>& p);
ObjOut<T>& operator= (T* p) {
_ptr = p;
return *this;
}
operator T& () {
return *_ptr;
}
operator T*& () {
return _ptr;
}
T* operator-> () {
assert (_ptr);
return _ptr;
}
T*& ptr () {
return _ptr;
}
};
Thx in advance.