?
Solved

pointer versus reference in the argument

Posted on 2016-08-29
7
Medium Priority
?
40 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

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 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41775817
I m sorry,  my overlook.  It is constructor accepting reference of type X
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 29

Accepted Solution

by:
pepr earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

719 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