473,545 Members | 524 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 3058
"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
1755
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
2173
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...
3
1889
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
1770
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...
2
22223
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
20805
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...
3
2104
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. ...
10
1614
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...
3
3843
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
7455
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...
0
7648
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. ...
0
7802
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...
0
7743
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...
0
5959
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...
0
3437
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1867
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
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
692
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...

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.