Solved

pointer versus reference in the argument

Posted on 2016-08-29
7
29 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

15 Experts available now in Live!

Get 1:1 Help Now