By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

memory and passing pointers

P: n/a
Hi,

I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.

Ivan

Sep 29 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
No. Copying pointers has no bearing on the object that they point to.
Sep 29 '06 #2

P: n/a
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?
Ron Natalie wrote:
Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.

No. Copying pointers has no bearing on the object that they point to.
Sep 29 '06 #3

P: n/a
Ivan Liu wrote:
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?

You are asking extremely vague questions. The main reasons references
exist are to accomodate overloading. If it were not for this C++
could have gotten by without them.

As for stylistically, it would depend on the situation. Nothing
is gained by switching from pointers to references blindly.
Sep 29 '06 #4

P: n/a
>>I'd like to ask if passing an object as an pointer into a function
>>evokes the copy constructor.
No. Copying pointers has no bearing on the object that they point
to.


So the efficiency is as good as passing a reference? If so why pass
reference?
Readability I'd say, although it is disputable. Some people prefer
passing pointers and some prefer passing reference. It's up to you to
chose your own style.

One place you really want to pass references instead of pointers is
overloaded operators.

Regards,
Ben
Sep 29 '06 #5

P: n/a
"Ivan Liu" <da*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Ron Natalie wrote:
>Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.

No. Copying pointers has no bearing on the object that they point to.
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?
Please don't top post in this newsgroup. Message rearranged.

The main reason I pass references into functions is so I can use . instead
of ->
Sep 29 '06 #6

P: n/a
Please don't top-post. Thank you. Rearranged.

Ivan Liu wrote:
Ron Natalie wrote:
Ivan Liu wrote:
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
No. Copying pointers has no bearing on the object that they point to.
Thanks for the reply.

So the efficiency is as good as passing a reference? If so why pass
reference?
There is a fundamental difference between passing references to objects
and passing pointers to objects. A reference must always refer to a
valid object. A pointer may refer to a valid object or may be null.
Therefore, if a function accepts a pointer, it must accept the
possibility of a null pointer and check the pointer value before using
it. Change the function to accept a reference and this extra
responsibility is removed. The function can safely assume that an
object is there.

The only time this is not an advantage is, of course, when, in your
design, the possibility of an object not existing makes sense. A
reference is no use here because it always referes to a valid object.
Pass a pointer and the function can query that pointer to see if it is
null or not.

A reference says: Here is the object for you to work with.
A pointer says: Here is an indicator that tells you whether the
optional object exists and, if so, where it is.

Summary: use references when you can and pointers when you must.

Gavin Deane

Sep 29 '06 #7

P: n/a
benben wrote:
>>I'd like to ask if passing an object as an pointer into a function
>>evokes the copy constructor.
>No. Copying pointers has no bearing on the object that they point
>to.
>

So the efficiency is as good as passing a reference? If so why pass
reference?

Readability I'd say, although it is disputable. Some people prefer
passing pointers and some prefer passing reference. It's up to you to
chose your own style.

One place you really want to pass references instead of pointers is
overloaded operators.

Two things about references that are not supported by pointers.

a) References can't be changed after being initialized
b) The lifetime of a reference can be transferred to objects.

(from an earlier post of mine)

#include <iostream>
#include <ostream>

struct A
{
A()
{
std::cout << "A default\n";
}

A( const A & )
{
std::cout << "A copy\n";
}

A & operator= ( const A & )
{
std::cout << "A operator =\n";
return * this;
}

~A()
{
std::cout << "A Destruct\n";
}

};

int main()
{
{
const A & a = A();

std::cout << "Temporary lives !\n";

A y( a );
}

std::cout << "Temporary is gone !\n";

{
const A * a = & A();

std::cout << "Temporary is gone already - a dangles!\n";
}

}
Sep 29 '06 #8

P: n/a
Gavin Deane wrote:
A pointer may refer to a valid object or may be null.
Therefore, if a function accepts a pointer, it must accept the
possibility of a null pointer and check the pointer value before using
it.
That's certainly not true. The C++ standard library itself is
full of functions that assume (they are not required to check)
that the caller is not passing a NULL or otherwise invalid pointer.

>
Sep 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.