473,769 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why I don't use references

Tom
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
can't get over:

3 - Can't tell which parms are references...
If I write a fn sig as 'void f( int x, int* y )', then the client code is
'f( a, &b )', so it is clear to everyone what is going on.
BUT, if I write a fn sig as 'void f( int x, int& y )', then the client code
is 'f( a, b )', and the compiler won't tell the coder that he has
misunderstood the function, and someone reviewing the code won't notice that
f changes 'b'.

Is there a solution to this?

TIA, Tom.
Jul 19 '05 #1
17 3080
"Tom" <No****@NoSpam. com> wrote in message
news:Q2******** **********@news 20.bellglobal.c om...
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.
And that's what a reference is supposed to be.

Now I'm sure there are good reasons for these first 2, but it's #3 that I
can't get over:

3 - Can't tell which parms are references...
If I write a fn sig as 'void f( int x, int* y )', then the client code is
'f( a, &b )', so it is clear to everyone what is going on.
BUT, if I write a fn sig as 'void f( int x, int& y )', then the client code is 'f( a, b )', and the compiler won't tell the coder that he has
misunderstood the function, and someone reviewing the code won't notice that f changes 'b'.

A programmer should check the function signature. Else how can he know what
arguments it takes?


Is there a solution to this?

Yes always check the function argument types and return type.



--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net

Jul 19 '05 #2
"Tom" <No****@NoSpam. com> wrote in...
The motivation for references seems clear: stop people from using nasty
pointers when all they really want is a reference to an object.
That's nonsense. If pointers were useless, they would have
been taken out of the language. If references were useless,
there would be no place for them, too.
But C++ references are so inadequate that I'm still using pointers for my
references to objects. [...]
Well, it's definitely not a good sign when one begins blaming
one's own inadequacies on the tools one has chosen to use...
Is there a solution to this?


A good C++ course, perhaps.

Victor
Jul 19 '05 #3
Tom <No****@NoSpam. com> wrote in message
news:Q2******** **********@news 20.bellglobal.c om...
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
can't get over:

3 - Can't tell which parms are references...
If I write a fn sig as 'void f( int x, int* y )', then the client code is
'f( a, &b )', so it is clear to everyone what is going on.
BUT, if I write a fn sig as 'void f( int x, int& y )', then the client code is 'f( a, b )', and the compiler won't tell the coder that he has
misunderstood the function, and someone reviewing the code won't notice that f changes 'b'.

Is there a solution to this?


Yes, you could simply not use references in such cases if you are
uncomfortable with them. Pointers have their uses and references have their
uses. Obviously, where you want the option of null or reseating, references
are not suitable. Where a function changes something via a function argument
there is, as you've explained, a good case for preferring a pointer. But
what about this case?

void f(const VeryLargeObject &obj);

Any reason to prefer a pointer here?

Also:
std::cout << "Result: " << result << std::endl;

How would this be done if a stream did not return a reference to itself from
its operator<<?

DW

Jul 19 '05 #4
"David White" <no@email.provi ded> wrote...
[...]
std::cout << "Result: " << result << std::endl;

How would this be done if a stream did not return a reference to itself from its operator<<?


I bet you the OP will tell you that operator overloading
is inadequate anyway, and the statement above should look
like

std::cout.print ("Result: ").println(resu lt);

Victor
Jul 19 '05 #5
Tom wrote:
The motivation for references seems clear:
to 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 can't get over:

3 - Can't tell which parms are references...
If I write a fn sig as 'void f( int x, int* y )',
then the client code is 'f(a, &b)',
so it is clear to everyone what is going on.


int a = 0;
int b = 0;
int* p = &b;
Jul 19 '05 #6
Tom
"David White" <no@email.provi ded> wrote in message news:R0******** ********@nasal. pacific.net.au. ..
Tom <No****@NoSpam. com> wrote in message
news:Q2******** **********@news 20.bellglobal.c om... .... Yes, you could simply not use references in such cases if you are
uncomfortable with them. Pointers have their uses and references have their
uses. Obviously, where you want the option of null or reseating, references
are not suitable. Where a function changes something via a function argument
there is, as you've explained, a good case for preferring a pointer. But
what about this case?

void f(const VeryLargeObject &obj);
Yes, I have no problem using references like this. I just don't tend to do it because, if I can only use references for a small % of my parameter passing needs, I feel it is better to always use pointers. More consistentency.

Thanks,
Tom.
Any reason to prefer a pointer here?

Also:
std::cout << "Result: " << result << std::endl;

How would this be done if a stream did not return a reference to itself from
its operator<<?

DW

Jul 19 '05 #7


Tom wrote:

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
can't get over:

3 - Can't tell which parms are references...
If I write a fn sig as 'void f( int x, int* y )', then the client code is
'f( a, &b )', so it is clear to everyone what is going on.
BUT, if I write a fn sig as 'void f( int x, int& y )', then the client code
is 'f( a, b )', and the compiler won't tell the coder that he has
misunderstood the function, and someone reviewing the code won't notice that
f changes 'b'.


I think the idea that 'pointer args return values' is itself misleading
if people use 'const' arguments (which is a good thing). Consider the
following function prototypes:

1) void MyFunc( MyType &x );
2) void MyFunc( const MyType &x );
3) void MyFunc( MyType *x );
4) void MyFunc( const MyType *x );

Whether I pass 'Y' or '&Y' tells me nothing about whether the function
can modify 'Y'.
Jul 19 '05 #8
"Victor Bazarov" <v.********@att Abi.com> wrote in message news:<Qb1Ra.729 71$N7.8517@sccr nsc03>...
"David White" <no@email.provi ded> wrote...
[...]
std::cout << "Result: " << result << std::endl;

How would this be done if a stream did not return a reference to itself

from
its operator<<?


I bet you the OP will tell you that operator overloading
is inadequate anyway, and the statement above should look
like

std::cout.print ("Result: ").println(resu lt);

Victor

I guess he'll want

std::cout->print("Resul t: ")->println(result );

which is more confusing, as the arrows point the wrong way.
Using std::cin with this style would look nice, though.

Regards,
--
Michiel Salters
Jul 19 '05 #9
"Victor Bazarov" <v.********@att Abi.com> wrote in message news:<Qb1Ra.729 71$N7.8517@sccr nsc03>...
"David White" <no@email.provi ded> wrote...
[...]
std::cout << "Result: " << result << std::endl;

How would this be done if a stream did not return a reference to itself

from
its operator<<?


I bet you the OP will tell you that operator overloading
is inadequate anyway, and the statement above should look
like

std::cout.print ("Result: ").println(resu lt);


And then again, what would the return type of
std::cout.print (const char*)
be? Perhaps a reference...?

Norbert
Jul 19 '05 #10

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

Similar topics

10
1783
by: Ricola ! | last post by:
Two c# dll's were compiled and added to the GAC. They appear in the .NET Configuration tool. However, when trying to add a reference to a new project, the dlls do not appear in the .NET property sheet. Why not?
16
2197
by: Jace Benson | last post by:
Ok I have read alot of things on zend.com, php.net and other sites went to the wikibooks to try to understand how to use a class. I have this project I want to do that I am sure would work great with a class. I just don't grasp the whole concept, and how to do it. I want to make a Collectable Card Game Draft Engine...(if any of you play VS System, LOTR, Magic: The Gathering, you know what I am talking about.) It would be way to...
3
1922
by: Andreas Fromm | last post by:
Hi, What is the problem with the following table declaration? CREATE TABLE persons ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, bdate DATE, address INTEGER REFERENCES addresses, phonepriv INTEGER REFERENCES phones,
3
1779
by: Melanie | last post by:
In AccessXP(A2000 mode), how do I make the change of references from ADO to DAO3.6 permanent? I go into references and uncheck ADO then scroll down to DAO3.6 and check it. I close references and reopen and DAO3.6 is in the list at the top and checked. Then I open my database and close it. When I go and look at references, ADO is back and checked and DAO3.6 is back in the list unchecked. BTW, the database was designed with DAO3.6 checked...
2
22291
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
458
21443
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,...
3
2116
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our code, but this becomes problematic when you want to build from multiple branches/working directories. Any thoughts on this would be appreciated. Thanks
10
1638
by: Summercool | last post by:
so many places, including the book PHP in a Nutshell, p. 80, it says: $a =& $b # set $a to reference $b if $a reference $b, then while you can say $b =1, you can't really say $a = 1. you need to say *($a) = 1 as in C or C++. Referencing must be coupled with dereferencing, and PHP is not doing the dereferencing, so why is it call referencing in the first place? (don't tell me PHP automatically dereference... as it will be really...
3
3864
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones are not used. I'd like to optimize the application ereasing the useless references. Thanks a lot.
0
9589
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
9423
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,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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
9865
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.