473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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.
Jul 22 '05 #1
3 1829
jimjim wrote:
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 <T> converting constructor taking a T*&.
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 <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.
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
<T> object was originally constructed.
Is there any constructor called when I pass the pointer ctx to the function?
The converting constructor "ObjOut (T*& p);".
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;
}
};


--
Regards,
Buster.
Jul 22 '05 #2
> 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
Jul 22 '05 #3
jimjim wrote:
If this is C++, given the absence of template typedef,
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.
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)?


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... ;)
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 :-(


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.

--
Regards,
Buster.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
12
by: John Henry | last post by:
Hi list, Just to make sure I understand this. Since there is no "pointer" type in Python, I like to know how I do that. For instance, if I do: ...some_huge_list is a huge list...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.