Solved

pointer versus reference in the argument

Posted on 2016-08-29
7
31 Views
Last Modified: 2016-09-19
Hi

I have noticed that certain function take a reference where others take a pointer in the argument of a function.


eg: A::A(X& x)
versus
     A::A(X* x)

what are the pros and cons of both? what would be the advantage of doing the reference over the pointer ?

thanks
0
Comment
Question by:LuckyLucks
7 Comments
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41775713
Reference is treated as constant pointer and can automatically dereferenced.

In your ex first one is copy constructor where as second is passing address of an object belonging to class A to class A's one arg constructor.

Pointer can be null but reference can't. If you want to pass null object to a function,  use pointer. In caller side,      by looking the call you can say it is pass by address/pointer or not.  But can't detect reference,  as it is also looking like call by value.
Ex  
void fun(a) ; // it can be pass by value or reference. Only can be determined by looking the header or source.
void fun(&a) ; // it is pointer no doubt

Reference to const also accepts temporary
void f(const T& t)
f(T(a, b, c)) ;
But pointer can't as you can't take address of temporary.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41775742
>>  copy constructor
Could you clarify why you say it is a  copy constructor
http://en.cppreference.com/w/cpp/language/copy_constructor

When you provide a list of buzz words without explaining what they are, then it is a good idea to provide a link that explains what it is.

>> as second is passing address of an object belonging to class A to class A's one arg constructor.
Then what is X?
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41775817
I m sorry,  my overlook.  It is constructor accepting reference of type X
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Accepted Solution

by:
pepr earned 500 total points (awarded by participants)
ID: 41775861
I personally prefer references wherever possible. It was not possible in the past because references were not available in the old C++.

With references, syntax of the source code is simpler. References are less error prone than pointers (because you have to care more in advance -- when writing the source). The source code is more readable. If the code was once a block of code that used the statically allocated object behind the identifier, then moving that code to the function body (during refactoring) is easier (less error prone) than rewriting to the same code that uses pointers.

There is indirect access hidden inside (that is the automatic dereference of the address), so still may be careful when efficiency is important -- but it is the same compared to to using a pointer instead.

I prefer to add const modifier whenever it makes sense. It is a bit more of work at the beginning, but you will get used to, and the more strict approach finally leads to more flexibility (possibly difficult to explain).
0
 

Author Comment

by:LuckyLucks
ID: 41777342
This is not correct:
"Reference is treated as constant pointer and can automatically dereferenced."


A reference is a de-referenced immutable(const) pointer. It's not can be, it already is. Perhaps you meant the same thing but the way it was written suggested a different meaning.
1
 
LVL 32

Expert Comment

by:phoffric
ID: 41777532
@Subrat
>> Reference is treated as constant pointer and can automatically dereferenced."

When you make statements like this using easy language, then to make your point, I suggest that you pose a concrete code snippet with comments to illustrate your statement.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now