473,734 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_contex t (Context_out res)
{
res = new Context ("");
}

and the way I invoke the function is:
Context_ptr ctx;
orb->get_default_co ntext(ctx);

So, what I believe it happens here is: I pass to the get_default_con text()
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_contex t (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_contex t (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 1856
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_contex t (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_co ntext(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_con text()
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_contex t (Context*& res).
It's equivalent to "void get_default_con text (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_contex t (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_con text (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_con text (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_con text (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_co ntext (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_con text (Context*& res) prototype)?


Maybe. The observable effect of 'orb->get_default_co ntext (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
5004
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
14
2536
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 pointers in some cases... TIA
110
9938
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 must be an object instead of
38
4627
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
2045
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 method creates a new object (a Matrix) for it. I suppose that after the new operator, my pointer is pointing to an object, so when the method has finished, the very first pointer is still poitint to the created method; however this is not working,...
51
4460
by: Kuku | last post by:
What is the difference between a reference and a pointer?
14
2412
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 think I need some "template". Sorry for my coding experience in c++
12
1636
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
3646
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.