memory and passing pointers

I

Ivan Liu

Hi,

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

Ivan
 
R

Ron Natalie

Ivan said:
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.
 
I

Ivan Liu

Thanks for the reply.

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

Ron Natalie

Ivan said:
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.
 
B

benben

I'd like to ask if passing an object as an pointer into a function
>

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
 
J

Jim Langston

Ivan Liu said:
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 ->
 
G

Gavin Deane

Please don't top-post. Thank you. Rearranged.

Ivan said:
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
 
G

Gianni Mariani

benben said:
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";
}

}
 
R

Ron Natalie

Gavin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top