how a pointer is passed by reference as a function argument?

J

jimjim

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.
 
B

Buster

jimjim said:
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;

If this is C++, given the absence of template typedef, we know that
Context_out is a class type.
the function implementation is:
void
CORBA::ORB::get_default_context (Context_out res)
{
res = new Context ("");
}

This takes res by value.
and the way I invoke the function is:
Context_ptr ctx;
orb->get_default_context(ctx);

The Context_ptr argument passed by the caller has to be copied into the
Context_out parameter received by the function. This is accomplished
using the ObjOut said:
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).

It's equivalent to "void get_default_context (ObjOut said:
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.

Passing a reference costs the same as passing a pointer. There is an
equivalence between the two. The main difference is simply the syntax.
However that's not exactly what happens here.
Is it really equivalent to writting
CORBA::ORB::get_default_context (Context*& res)?

Close to it. The ObjOut class template is a very thin wrapper for a
reference to a pointer. When you pass a ObjOut <T> by value, its member,
_ptr, is copied. Then there are several objects of type ObjOut <T> whose
_ptr members refer to the same object of type T*. Whatever changes are
made to the _ptr member of any of these ObjOut <T> objects (via the
ObjOut member functions) apply to the T* object from which the ObjOut
Is there any constructor called when I pass the pointer ctx to the function?

The converting constructor "ObjOut (T*& p);".
 
J

jimjim

If this is C++, given the absence of template typedef,

What do you mean by saying "if this is C++", and the "absence of template
typedef"? How a template typedef as such would looked like?


As I didn't fully understand the process, I will try to summarize and please
do correct me when I am wrong:

ctx is a pointer which is passed by value as a function argument. The actual
function prototype is:
void get_default_context (ObjOut <Context> res)

As ctx value has to be copied to an ObjOut <Context> type parameter, the
ObjOut (T*& p) constructor is called. This happens in such cases in which
values have to be copied to Object type parameters.

#is there any particular name for this C++ mechanism of "copying" arguments
to Object type parameters or Class templete parameters? it seems I cannot
find good references on the net#

So, as the constructor takes Context*& it is actually a reference to a
pointer (as you mentioned).

However, why is that the private member is T*& _ptr; rather than T* _ptr ?

How the reference to a pointer is then copied to the res which is used in
the function's body? Isn't res of type ObjOut? how is it possible to assign
an object of type Context to a variable of type ObjOut (I refer to the res =
new Context ("") )
The ObjOut class template is a very thin wrapper for a reference to a
pointer.

are there any runtime costs due to this implementation (rather than having a
get_default_context (Context*& res) prototype)?

Why should I introduce such a wrapper in my implemantation?
Then there are several objects of type ObjOut <T> whose
_ptr members refer to the same object of type T*. Whatever changes are
made to the _ptr member of any of these ObjOut <T> objects (via the
ObjOut member functions) apply to the T* object from which the ObjOut
<T> object was originally constructed.

can't understand this at all :-(

thx in advance

Regards,
jimjim
 
B

Buster

jimjim said:
As I didn't fully understand the process, I will try to summarize and please
do correct me when I am wrong:

ctx is a pointer which is passed by value as a function argument. The actual
function prototype is:
void get_default_context (ObjOut <Context> res)

As ctx value has to be copied to an ObjOut <Context> type parameter, the
ObjOut (T*& p) constructor is called. This happens in such cases in which
values have to be copied to Object type parameters.

That's one possibility. The other is that the ObjOut (T*& p) constructor
initializes a temporary object, then the copy constructor is invoked to
initialize the parameter from the temporary. Either way is allowed, but
there must be an accessible copy constructor.
#is there any particular name for this C++ mechanism of "copying" arguments
to Object type parameters or Class templete parameters? it seems I cannot
find good references on the net#

It's an instance of "copy initialization" (we say the parameter is copy
initialized by the argument).
So, as the constructor takes Context*& it is actually a reference to a
pointer (as you mentioned).

However, why is that the private member is T*& _ptr; rather than T* _ptr ?

The observable effect of 'orb->get_default_context (ctx)' is the same
as 'ctx = new Context ("")'.

The 'T*& _ptr' member of ObjOut <T> is just part of the set-up to
accomplish this. 'T** _ptr' could be made to work instead but 'T* _ptr'
could not.
How the reference to a pointer is then copied to the res which is used in
the function's body? Isn't res of type ObjOut? how is it possible to assign
an object of type Context to a variable of type ObjOut (I refer to the res =
new Context ("") )

'res = new Context ("")' is a call to an overloaded assignment operator
defined in ObjOut. Look at the implementation of that function: it is
the line '_ptr = p' which finally changes the value of ctx.
are there any runtime costs due to this implementation (rather than having a
get_default_context (Context*& res) prototype)?

Maybe. The observable effect of 'orb->get_default_context (ctx)' is just
the same as if you had 'ctx = new Context ("")' instead. Perhaps your
optimizer can make the simplification. (There's no reason not to do it
yourself in this case - your code would be much easier to understand.)
Why should I introduce such a wrapper in my implemantation?

I'm not psychic... ;)
can't understand this at all :-(

It's reasonably advanced stuff. You can't learn C++ from the net, you
should at least get a good book. Unfortunately there are a lot of bad
and outdated books around. I believe there are book reviews at the
website of ACCU.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top