tags:

views:

79

answers:

4

hello, can I have this snippet of the code:

C *pa1 = new C(c2);

and I transfer it to another function:

foo(pa1);

what exactly do I transfer actual pointer or its copy, thanks in advance and can somebody give some info about in which cases info is copied, and in which I transfer actual pointer

declaration of foo:

foo(A const *pa)
+1  A: 

Since pa1 is of type pointer-to-C, you are passing a pointer to C to the function foo. You are not copying the actual object.

To pass an object, you would need foo to take an object of type C and to dereference pa1 when passing it:

void foo(C);
...
foo(*pa1);
camh
but what about pointer itself, do I copy it or not?
lego69
The pointer is copied. In the function you call foo from, it holds a pointed to C (in pa1). In foo, it holds a pointer to C (in its actual argument). Hence the pointer is copied. Is that what you mean?
camh
A: 

Since pa1 is a pointer (as you have defined it as a C *), you are passing a pointer to the C object into foo.

That said, whether you are passing the pointer by value or by reference is unknown without seeing the declaration of foo.

R Samuel Klatchko
can You give me please two declarations which will show me difference between these two methods of transferring in this case
lego69
+1  A: 

Assuming foo is declared as:

void foo(C* p);

you are passing a copy of the pointer.

This means, if foo does this:

p = &some_other_object;

that change to the pointer won't be seen by the caller.

It also means we're copying the pointer, not the thing pointed to. If foo does this:

p->bar = "Smurf!"

pa1 in the caller will also see the change. For this reason, pointers are often used to implement a kind of pass-by-reference.

If foo were declared:

void foo(C*& p);

then p would be a reference to pa1, and changes to p would result in changes to pa1. Historically, this has also been implemented using pointers to pointers:

void foo(C** p);

in which case you call foo like this:

foo(&pa1);

and foo can do something like:

*p = &some_other_object;

to change what pa1 points to.

Owen S.
ok, thanks, but what if I want to transfer actual pointer? not its copy, do I need use two **?
lego69
camh
@camh: or `void foo( C*` and there is no need to change the call.
David Rodríguez - dribeas
A: 
C *pa1;

object of type 'C*' is created on the stack (local variable pa1)

pa1 = new C(c2);

object of type 'C' is created on the heap, its address is returned into 'pa1' variable

foo(pa1)

We can say "object of type 'C*' (variable pa1) is passed by-value to function foo, the function gets a copy of the pointer",
But also "object of type 'C' (this one created on the heap) passed by-reference (through pointer) to function foo, no copy 'C' object is made.

To pass by-reference (without making a copy of an object) either c++ reference to the object can be used or pointer to the object. They act similar when going about function parameter passing, i.e all of 4 lines below gives the same effect, passing by-reference (these are const references, remove const keyword to be able to permanently modify the object by the foo function):

foo(const A *pa) { pa->DoSth(); } /*...*/ foo(some_pointer);
foo(const A *pa) { pa->DoSth(); } /*...*/ foo(&some_reference);
foo(const A &pa) { pa.DoSth(); } /*...*/ foo(*some_pointer);
foo(const A &pa) { pa.DoSth(); } /*...*/ foo(some_reference);
adf88