Solved

pointer versus reference in the argument

Posted on 2016-08-29
7
32 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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