From: souravsain on
Hi

When will you choos to pass by reference over const pointer and vise
versa?

And why can't a copy constructor be declared like
A(const A* const p) instead of A(const A& r) (any reason other than
"it had to be one way", i mean any technical / language limitation?

If we had a previous discussion on this topic (which I tried to find
but could not) please provide the link.

Thanks.
Sourav

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alf P. Steinbach /Usenet on
* souravsain, on 02.07.2010 23:26:
> Hi

Hi.

The questions below sound like homework questions.

But I'll answer them anyway.


> When will you choos to pass by reference over const pointer and vise
> versa?

First of all there may be a silly C++ coding guideline, such as Google's, that doesn't leave you much room for making reasonable choices.

But if you're free to choose, then consider that the point of the "++" in C++, compared to its ancestor language C, is stronger static type checking, and that the point of static type checking is to translate runtime errors or incorrect results, to compile time errors, in order to save total development time.

Hence you want the type that helps you the most to obtain correct code.

Since a pointer allows nullvalues, you generally don't want the pointer type unless nullvalues are meaningful and intentionally allowed.

However, there are some exceptional cases. One is for a raw character string. Another is where a pointer to the client supplied object is stored somewhere by the function. In this last case you do not want to allow the client code to supply a temporary object. And in that case you do want to clearly convey that what's passed is really a pointer -- remember that code is about communicating to other programmers, not about communicating to the compiler.


> And why can't a copy constructor be declared like
> A(const A* const p) instead of A(const A& r) (any reason other than
> "it had to be one way", i mean any technical / language limitation?

There are some special rules for copy constructors -- calls can be optimized away in certain situations, copy constructors can be automatically generated, a templated constructor is not a copy constructor -- but these special rules have nothing to do with the declaration.

Rather the declaration is simply any declaration that permits normal copy constructor's usage, in particular for copying from an lvalue without applying the address operator. For a class A passing an A by value is however excluded, since it would itself involve copying of an A instance. This leaves 'A&', 'A const&', 'A volatile&' and 'A const volatile&' as possible first argument types.

E.g. all the following are copy constructors:

A( A const& );
A( A& )
A( A const&, int = 42 );

The middle one is special in that it doesn't support a temporary object as actual argument. This is the copy constructor used by std::auto_ptr.

The variations with 'volatile' are mainly only meaningful for intricate uses of 'volatile' as a type deduction feature.


> If we had a previous discussion on this topic (which I tried to find
> but could not) please provide the link.

Don't know, sorry.


Cheers & hth.,

- Alf


--
blog at <url: http://alfps.wordpress.com>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Uzdavinis on
On Jul 2, 4:26 pm, souravsain <souravs...(a)gmail.com> wrote:
> Hi
>
> When will you choos to pass by reference over const pointer and vise
> versa?

Though they are very similar in concept, they have some very different
properties that you should consider:

1) pointers can be null, references must be initialized
2) pointers can change what they point to, references cannot be
rebound
3) references to temporaries keep the temporary alive for the life of
the
(original) reference, pointers don't. (Note: copying one reference
to
another does not extend the lifetime, however, like passing a
temporary
through a constructor argument, to initialize a member
reference...
not good idea as it can dangle.)
4) the syntax is different, and possibly the main initial reason for
adding
references.

References were initially added in conjunction with overloading
operators. If
you had to use pointers, then you couldn't have natural syntax like:

x = y + z;

When x, y, and z are user defined classes. You would have to do
something like

x.assign(y.add(&z));

And give up operator overloads and working on objects and not their
addresses.


> And why can't a copy constructor be declared like
> A(const A* const p) instead of A(const A& r) (any reason other than
> "it had to be one way", i mean any technical / language limitation?

A pointer adds explicit indirection, while a reference gives the
appearance
of direct access. Copying the address of an object is not the same
thing as
copying the object. If C++ only took pointers for copy constructors,
then
how would you give the address of an object to a constructor, aside
from
adopting some idiom of an extra dummy parameter to make it not be the
copy ctor?

Also, the syntax again would be ghastly.

Foo f;
Foo g(&f); // g is a copy of f??

What about

foo g(0); // null

I guess the point I'm making is, it's not just arbitrary, and I can
think of
no good reasons to choose the pointer over the reference for this
signature.


--
Chris


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]