1

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)
lego69
  • 767
  • 1
  • 12
  • 20
  • I wrote a long explanation of the different argument semantics in a previous question that you might want to take a look at. http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139553#2139553 – David Rodríguez - dribeas Jun 15 '10 at 07:38

4 Answers4

1

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
  • 40,988
  • 13
  • 62
  • 70
  • 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 Jun 15 '10 at 06:35
1

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.
  • 7,665
  • 1
  • 28
  • 44
  • ok, thanks, but what if I want to transfer actual pointer? not its copy, do I need use two **? – lego69 Jun 15 '10 at 06:37
  • I think you need to explain what you mean by "transfer". If you want a function to be able to modify the pointer so the caller's pointer changes, then yes you do want C** instead of C*, and call foo(&pa1). – camh Jun 15 '10 at 06:41
  • @camh: or `void foo( C*& p );` and there is no need to change the call. – David Rodríguez - dribeas Jun 15 '10 at 07:35
0

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
  • 74,869
  • 16
  • 134
  • 187
  • can You give me please two declarations which will show me difference between these two methods of transferring in this case – lego69 Jun 15 '10 at 06:34
0
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
  • 4,277
  • 1
  • 23
  • 21