Solved

pointer versus reference in the argument

Posted on 2016-08-29
7
33 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 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 29

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 202
Best book to learn C++ 4 84
Header of docx file 17 118
Which Linux flavors will this run on? 6 96
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. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

830 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