473,804 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

references and pointers

MQ
Can someone tell me where I should use pointers and where I should use
references? In his book, Stroustrup says that you should use pointers
for passing arguments that are to be modified, not references. Yet,
in an interview, he laments the overuse of pointers and such in code.
It seems contradictory to me. Why should we not use references
instead of pointers? Can someone give me an idea of best practices in
this respect?

regards,
B.

Nov 4 '07 #1
19 1994
MQ wrote:
Can someone tell me where I should use pointers and where I should use
references? In his book, Stroustrup says that you should use pointers
for passing arguments that are to be modified, not references. Yet,
in an interview, he laments the overuse of pointers and such in code.
It seems contradictory to me. Why should we not use references
instead of pointers? Can someone give me an idea of best practices in
this respect?
Think about the basic difference between references and pointers:
Pointers can be made to point to something else than what they were
initialized to point to (including null) while references can't.

This, IMO, gives a good rule of thumb for the decision: If you don't
need to change where the thing is pointing, use references, else use
pointers.
Nov 4 '07 #2
On Nov 4, 11:20 am, Juha Nieminen <nos...@thanks. invalidwrote:
MQ wrote:
Can someone tell me where I should use pointers and where I should use
references? In his book, Stroustrup says that you should use pointers
for passing arguments that are to be modified, not references. Yet,
in an interview, he laments the overuse of pointers and such in code.
It seems contradictory to me. Why should we not use references
instead of pointers? Can someone give me an idea of best practices in
this respect?

Think about the basic difference between references and pointers:
Pointers can be made to point to something else than what they were
initialized to point to (including null) while references can't.

This, IMO, gives a good rule of thumb for the decision: If you don't
need to change where the thing is pointing, use references, else use
pointers.
Why then does Stroustrup make the following statement in his book:

"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"

This says to me that we in fact should *not* use references in this
way


Nov 4 '07 #3
bo*******@gmail .com wrote:
On Nov 4, 11:20 am, Juha Nieminen <nos...@thanks. invalidwrote:
>MQ wrote:
>>Can someone tell me where I should use pointers and where I should use
references? In his book, Stroustrup says that you should use pointers
for passing arguments that are to be modified, not references. Yet,
in an interview, he laments the overuse of pointers and such in code.
It seems contradictory to me. Why should we not use references
instead of pointers? Can someone give me an idea of best practices in
this respect?
Think about the basic difference between references and pointers:
Pointers can be made to point to something else than what they were
initialized to point to (including null) while references can't.

This, IMO, gives a good rule of thumb for the decision: If you don't
need to change where the thing is pointing, use references, else use
pointers.

Why then does Stroustrup make the following statement in his book:

"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"
He made a mistake? Or possibly idiomatic style has changed over time.

--
Ian Collins.
Nov 4 '07 #4
On Nov 4, 12:00 pm, Ian Collins <ian-n...@hotmail.co mwrote:
boroph...@gmail .com wrote:
On Nov 4, 11:20 am, Juha Nieminen <nos...@thanks. invalidwrote:
MQ wrote:
Can someone tell me where I should use pointers and where I should use
references? In his book, Stroustrup says that you should use pointers
for passing arguments that are to be modified, not references. Yet,
in an interview, he laments the overuse of pointers and such in code.
It seems contradictory to me. Why should we not use references
instead of pointers? Can someone give me an idea of best practices in
this respect?
Think about the basic difference between references and pointers:
Pointers can be made to point to something else than what they were
initialized to point to (including null) while references can't.
This, IMO, gives a good rule of thumb for the decision: If you don't
need to change where the thing is pointing, use references, else use
pointers.
Why then does Stroustrup make the following statement in his book:
"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"

He made a mistake? Or possibly idiomatic style has changed over time.

--
Ian Collins.
Are you serious? This is in the most recent version of "The C++
programming language", and he mentions this point in several
locations. Hardly an editing mistake. Let me just state that I am
not debating that the point he makes seems right, it does not seem
right to me, but it seems to be more than just a mistake.

Nov 4 '07 #5
bo*******@gmail .com wrote:
On Nov 4, 12:00 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>boroph...@gmai l.com wrote:
>>Why then does Stroustrup make the following statement in his book:
"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"
He made a mistake? Or possibly idiomatic style has changed over time.
*Please* don't quote signatures.
>
Are you serious? This is in the most recent version of "The C++
programming language", and he mentions this point in several
locations. Hardly an editing mistake. Let me just state that I am
not debating that the point he makes seems right, it does not seem
right to me, but it seems to be more than just a mistake.
http://www.research.att.com/~bs/bs_f...and-references

Puts a different view across, so one of my guesses was probably true.

I'd only recommend using pointers when the argument can meaningfully be
NULL, or when interfacing with legacy code.

--
Ian Collins.
Nov 4 '07 #6
On Nov 3, 8:15 pm, Ian Collins <ian-n...@hotmail.co mwrote:
I'd only recommend using pointers when the argument can meaningfully be
NULL, or when interfacing with legacy code.
Or when you need to store a "reference" (for lack of a better term) to
a shared object in a class that must be STL container-compatible
(assignable).

I admit that I am newish to C++, but the pattern I have found is that
I only use references when passing arguments. I try hard never to use
"out" variables, but when I do, I use pointers.

Nov 4 '07 #7

"Ian Collins" <ia******@hotma il.comwrote in message
news:5p******** *****@mid.indiv idual.net...
bo*******@gmail .com wrote:
>On Nov 4, 12:00 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>>boroph...@gma il.com wrote:
>>>Why then does Stroustrup make the following statement in his book:
"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"
He made a mistake? Or possibly idiomatic style has changed over time.
*Please* don't quote signatures.
>>
Are you serious? This is in the most recent version of "The C++
programming language", and he mentions this point in several
locations. Hardly an editing mistake. Let me just state that I am
not debating that the point he makes seems right, it does not seem
right to me, but it seems to be more than just a mistake.
http://www.research.att.com/~bs/bs_f...and-references

Puts a different view across, so one of my guesses was probably true.

I'd only recommend using pointers when the argument can meaningfully be
NULL, or when interfacing with legacy code.
I was actually debating this with myself 2 days ago. Someone gave me some
code work on, he was using pointers to modify the parameters. I started to
change them to references, then realized that in mainline, there is no
indication if the parameter was going to be changed or not.

Consider.

void foo( int* Val )
{
*Val = 23;
}

void bar( int& Val )
{
Val = 23;
}

int main()
{
int MyInt = 10;
foo( &MyInt );
bar( MyInt );
}

Becaue Foo forces us to take the address of MyInt, it is fairly obvious in
mainline that MyInt is probably going to be changed, else why pass the
address of a simple int? bar however gives no indication in mainline that
MyInt will be changed.

Maybe that is the reasoning behind it.

Reguardless, I did go ahead and change them all to references anyway.
Nov 4 '07 #8
Jim Langston wrote:
"Ian Collins" <ia******@hotma il.comwrote:
>http://www.research.att.com/~bs/bs_f...and-references

Puts a different view across, so one of my guesses was probably true.

I'd only recommend using pointers when the argument can meaningfully be
NULL, or when interfacing with legacy code.

I was actually debating this with myself 2 days ago. Someone gave me some
code work on, he was using pointers to modify the parameters. I started to
change them to references, then realized that in mainline, there is no
indication if the parameter was going to be changed or not.

Consider.

void foo( int* Val )
{
*Val = 23;
}

void bar( int& Val )
{
Val = 23;
}

int main()
{
int MyInt = 10;
foo( &MyInt );
bar( MyInt );
}

Becaue Foo forces us to take the address of MyInt, it is fairly obvious in
mainline that MyInt is probably going to be changed, else why pass the
address of a simple int? bar however gives no indication in mainline that
MyInt will be changed.
A good example of why you should give functions meaningful names.

--
Ian Collins.
Nov 4 '07 #9
On 2007-11-04 01:52:26 -0400, "Jim Langston" <ta*******@rock etmail.comsaid:
>
"Ian Collins" <ia******@hotma il.comwrote in message
news:5p******** *****@mid.indiv idual.net...
>bo*******@gmail .com wrote:
>>On Nov 4, 12:00 pm, Ian Collins <ian-n...@hotmail.co mwrote:
boroph...@gm ail.com wrote:
>>>>Why then does Stroustrup make the following statement in his book:
"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"
He made a mistake? Or possibly idiomatic style has changed over time.
*Please* don't quote signatures.
>>>
Are you serious? This is in the most recent version of "The C++
programming language", and he mentions this point in several
locations. Hardly an editing mistake. Let me just state that I am
not debating that the point he makes seems right, it does not seem
right to me, but it seems to be more than just a mistake.
http://www.research.att.com/~bs/bs_f...and-references

Puts a different view across, so one of my guesses was probably true.

I'd only recommend using pointers when the argument can meaningfully be
NULL, or when interfacing with legacy code.

I was actually debating this with myself 2 days ago. Someone gave me some
code work on, he was using pointers to modify the parameters. I started to
change them to references, then realized that in mainline, there is no
indication if the parameter was going to be changed or not.

Consider.

void foo( int* Val )
{
*Val = 23;
}

void bar( int& Val )
{
Val = 23;
}

int main()
{
int MyInt = 10;
foo( &MyInt );
bar( MyInt );
}

Becaue Foo forces us to take the address of MyInt, it is fairly obvious in
mainline that MyInt is probably going to be changed, else why pass the
address of a simple int? bar however gives no indication in mainline that
MyInt will be changed.

Maybe that is the reasoning behind it.

Reguardless, I did go ahead and change them all to references anyway.
I think you made a good call to change them to references anyway.
Otherwise, you would have to worry about people passing invalid pointer
like in foo(0).

So, like Ian Collins had stated in another post, use pointers only if
there is a need to include NULL as a possible value. But even in that
case, perhaps it is cleaner for the class to implement a singleton
object that represents the "NULL" object of the class anyway.

I'm still a newbie in C++, but I'm getting the sense that proper use of
STL should eliminate all needs of pointers. However, for efficiency
purposes, perhaps pointers should sometimes be employed, just like
'goto' is theoretically not needed but may be practically needed.

--

-kira

Nov 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3084
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using pointers for my references to objects. There are 3 reasons why I find them inadequate: 1 - Can't be null. 2 - Can't be reseated. Now I'm sure there are good reasons for these first 2, but it's #3 that I
15
14042
by: Web Developer | last post by:
Hi, Can someone provide a short and concise statement(s) on the difference between pointers and references. A graphical representation (via links?) of both would be much appreciated as well. WD
8
2275
by: john townsley | last post by:
are there any differences when using pointers or passing by reference when using 1) basic variables 2)complex variable types like arrays,structures it seems easier to simply pass by reference and simply forget about pointers but thats seems to easy, there must still be a need for pointers
11
1994
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
458
21533
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
28
2042
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just recently, a poster posted looking for an explanation of references. I'll give my own understanding if it's worth anything. First of all, the C++ Standard is a very flexible thing. It gives a mountain of freedom to implementations to do things...
9
2476
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you deal with references which are local variables. But references can also be function arguments (in fact they are more useful this way) in which case it has to have the in-memory representation.
76
4729
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the pointers are allowed to be null, while references must refer an existing variable of required type. The null is normally used for making optional parameters. But there is no way to pass null reference in C#. Something is missing.
1
1402
by: sip.address | last post by:
Hi everyone, A while ago I asked for help about using smart pointers (ie. shared_ptr) in cyclic situations. Weak ptrs popped out and I gave it a try, but to be honest, I don't feel very comfortable with them. Would using references be a bad choice? The more I think about it the more I realize it shouldn't be any problem with them, so could it be a good use case?
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.