| re: how a pointer is passed by reference as a function argument?
jimjim wrote:[color=blue][color=green]
>>If this is C++, given the absence of template typedef,[/color]
>
> 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.[/color]
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.
[color=blue]
> #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#[/color]
It's an instance of "copy initialization" (we say the parameter is copy
initialized by the argument).
[color=blue]
> 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 ?[/color]
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.
[color=blue]
> 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 ("") )[/color]
'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.
[color=blue][color=green]
>>The ObjOut class template is a very thin wrapper for a reference to a
>> pointer.[/color]
>
> are there any runtime costs due to this implementation (rather than having a
> get_default_context (Context*& res) prototype)?[/color]
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.)
[color=blue]
> Why should I introduce such a wrapper in my implemantation?[/color]
I'm not psychic... ;)
[color=blue][color=green]
>>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.[/color]
>
> can't understand this at all :-([/color]
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. |