473,698 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constant references

Are they possible? I am passing in a large array of Bytes, thus I
don't want to use ByVal.

Zytan

Feb 1 '07
32 1476
Zytan wrote:
<snip>
The reason "const" exists is to prevent me from making
mistakes. Even the smartest of us make mistakes. The compiler helps
us avoid pitfalls with concepts like "const" and encapsulation. They
force good programming. That's why good programmers use them. I
can't even begin to explain how astonished I am at your reply...
I am further astonished that VB offers no way to protect programmers
from being stupid with passing in arrays. And that even C# doesn't
have this. I am just amazed... the fact that all VB programmers out
there in VB land are programming without this protection... and now
your reply makes more sense to me. Because it's the only solution, it
is the only way you CAN think about it. Amazing. (that is... unless
the proxy objects that Herfried mentioned are a solution...?)
Unfortunatelly, there's no const propagation *in .Net*, as far as I
can't tell (I guess C++ programs that use const like this aren't CLS
compatible).

The only resort for the mindfull programmer is strong faith or relying
on the "AsReadOnly " that certain objects expose. But then,
unfortunately, the erroneous access will only be caught at run time,
not a compile time as it should be...

Sad, indeed...

Regards,

Branco.

Feb 2 '07 #11
On Feb 1, 6:36 pm, "Zytan" <zytanlith...@y ahoo.comwrote:
Stephany,
This is one of those:
Patient: "Doctor! It hurts when I do this."
Doctor: "Well don't do that!"
questions.
If you don't want the passed in array changed in your function, then don't
change it in your function!

No, no, no! This is very, very bad way of thinking about it. My
program doesn't change the array inside of the function. Really. I'm
a good programmer. Trust me. But, the point is... I don't trust
myself. The reason "const" exists is to prevent me from making
mistakes. Even the smartest of us make mistakes. The compiler helps
us avoid pitfalls with concepts like "const" and encapsulation. They
force good programming. That's why good programmers use them. I
can't even begin to explain how astonished I am at your reply...

I am further astonished that VB offers no way to protect programmers
from being stupid with passing in arrays. And that even C# doesn't
have this. I am just amazed... the fact that all VB programmers out
there in VB land are programming without this protection... and now
your reply makes more sense to me. Because it's the only solution, it
is the only way you CAN think about it. Amazing. (that is... unless
the proxy objects that Herfried mentioned are a solution...?)

Zytan
Zytan,

Even in C++ const didn't guarentee much since the reference's
constness could be casted away. I think it was left out of C# because
the cost versus benefit of it tipped toward the unfavorable
direction. Implementing const reference parameters would add a
substantial amount of complexity to the language.

The best way to guarentee that an object's state cannot be changed is
to make it immutable.

Another strategy would be to use an interface or proxy object.
Though, I don't think either could absolutely be enforced at compile
time. A proxy object would probably be the best of two.

Brian

Feb 2 '07 #12
On Feb 1, 3:24 pm, "Zytan" <zytanlith...@y ahoo.comwrote:
Are they possible? I am passing in a large array of Bytes, thus I
don't want to use ByVal.

Zytan
Have you tried using ByRef? Look up passing variables ByRef for
VB .NET.

Feb 2 '07 #13
Herfried,

Thanks proxy object code sample. (I just learned how the Default
keyword works... neat!)
Dim Names() As String = ...
Foo(..., New ArrayProxy(Of String())(Names )), ...)
I think you mean: Note the lack of () after "String". I couldn't get
it to compile your way.
Foo(..., New ArrayProxy(Of String)(Names)) , ...)
Ok... this solution is lacking in that the Foo function cannot discern
the length of the array. Ouch. Not only that, but to pass in the
length of the array to Foo, the caller can also not discern its length
for the same reason. You must use the length of the array that it was
constructed from (Names.Length, in this case). It's ugly. Also, all
of this mess just to do what "const" does in C++ <sigh It's just all
ugly... I can't help it, it's the truth.

But, thanks for your input, Herfried. It's nice to be able to
consider all possibilities.

Zytan

Feb 2 '07 #14
Stephany,
Yes you're absolutely right - the best of us do make mistakes.

That is why we have robust debugging and testing techniques.
Yes.
Checking that some variable that shouldn't be modified hasn't been is the
most the most basic of those techniques.
No. Leaving such a simple, laborious task to the compiler is one of
the basic of those techniques.

No human can possibly read code like English, and look over a function
in a swift manner, and determine that the constant variables are
unchanging. What happens if one of them is passed to another
function? You have to either look at that function's code, and all
it's inner function calls, all the way down the stack / tree, and
then, if you haven't missed anything (and, since when is any human
perfect), you can determine that the code is correct. If you're
incorrect, hopefully you'll catch the bug during runtime development
and not after deployment, where it's 100 times more costly to fix than
during runtime development.

OR... you could use "const" -- 5 letters -- and let the compiler
notify you during COMPILE time which is 1/10th as costly as catching
it during beta testing. The compiler can go through all your code,
and determine at all levels that the variable is not being changed.
You can create "const" class functions to ensure the class data
members remain unchanged, and these are only allowed to be call other
functions that promise the same thing. All caught at compile time.

This is several ORDERS OF MAGNITUDE more robust than "human checking".

Besides, who has time to check code? Who does this? Even if you
*could* check all that code, and were as perfect as a machine, why
would you want to waste your time doing this? The time would be
better spend writing more robust code, letting the compiler check all
of that for you, removing any worries / intellectual barriers that you
may have about potential broken code.

Zytan

Feb 2 '07 #15
Brian,
Even in C++ const didn't guarantee much since the reference's
constness could be casted away.
No. It guarantees a LOT. I use it regularly on large projects. It
helps enforce design issues where certain elements of my code just
should not be allowed to mess with certain data. It is extremely
important. Also, for all the elsewhere mentioned reasons in another
post of mine, such as catching bugs at compile time, make it
priceless. It just forces good programming. How can you beat that?

Yes, you can cast it away -- but not with 'normal' C++ code. Casting
away const is not allowed. You can get around it with fancy C++ code
to trick the compiler into accessing that memory, but it's really just
a high level way of writing assembly. So, yes, it's not 100% bullet
proof. But, that's not the point. The point is the immense gain you
get, from being forced to write good code, from using it.

Just because someone can pick a lock, it doesn't mean you should stop
locking your door. Now imagine a lock that forces the door to be
locked, and forces you to have the keys on you, every time you leave.
Anything that forces good habits is a good idea.

I think it was left out of C# because
the cost versus benefit of it tipped toward the unfavorable
direction. Implementing const reference parameters would add a
substantial amount of complexity to the language.
Really? I don't really know. It's just sad that it doesn't exist...

The best way to guarantee that an object's state cannot be changed is
to make it immutable.
Ok. Something else for me to learn. Any places I can get started on
researching this? Any help is appreciated.

Another strategy would be to use an interface or proxy object.
Though, I don't think either could absolutely be enforced at compile
time. A proxy object would probably be the best of two.
Thanks for the suggestion. This was mentioned above, and for
something where it is critical (i.e. it warrants the extra hassle
involved), it certainly could be used.

Zytan

Feb 2 '07 #16
Well, all I can really say is ...

If you need the features exposed by the C++ language and the C++ compiler so
badly then I don't understand why you are not using that instead of bleating
on about something that just "ain't gonna happen".
"Zytan" <zy**********@y ahoo.comwrote in message
news:11******** **************@ a34g2000cwb.goo glegroups.com.. .
Stephany,
>Yes you're absolutely right - the best of us do make mistakes.

That is why we have robust debugging and testing techniques.

Yes.
>Checking that some variable that shouldn't be modified hasn't been is the
most the most basic of those techniques.

No. Leaving such a simple, laborious task to the compiler is one of
the basic of those techniques.

No human can possibly read code like English, and look over a function
in a swift manner, and determine that the constant variables are
unchanging. What happens if one of them is passed to another
function? You have to either look at that function's code, and all
it's inner function calls, all the way down the stack / tree, and
then, if you haven't missed anything (and, since when is any human
perfect), you can determine that the code is correct. If you're
incorrect, hopefully you'll catch the bug during runtime development
and not after deployment, where it's 100 times more costly to fix than
during runtime development.

OR... you could use "const" -- 5 letters -- and let the compiler
notify you during COMPILE time which is 1/10th as costly as catching
it during beta testing. The compiler can go through all your code,
and determine at all levels that the variable is not being changed.
You can create "const" class functions to ensure the class data
members remain unchanged, and these are only allowed to be call other
functions that promise the same thing. All caught at compile time.

This is several ORDERS OF MAGNITUDE more robust than "human checking".

Besides, who has time to check code? Who does this? Even if you
*could* check all that code, and were as perfect as a machine, why
would you want to waste your time doing this? The time would be
better spend writing more robust code, letting the compiler check all
of that for you, removing any worries / intellectual barriers that you
may have about potential broken code.

Zytan
Feb 2 '07 #17
On Feb 2, 11:58 am, "Zytan" <zytanlith...@y ahoo.comwrote:
Brian,
Even in C++ const didn't guarantee much since the reference's
constness could be casted away.

No. It guarantees a LOT. I use it regularly on large projects. It
helps enforce design issues where certain elements of my code just
should not be allowed to mess with certain data. It is extremely
important. Also, for all the elsewhere mentioned reasons in another
post of mine, such as catching bugs at compile time, make it
priceless. It just forces good programming. How can you beat that?

Yes, you can cast it away -- but not with 'normal' C++ code. Casting
away const is not allowed. You can get around it with fancy C++ code
to trick the compiler into accessing that memory, but it's really just
a high level way of writing assembly. So, yes, it's not 100% bullet
proof. But, that's not the point. The point is the immense gain you
get, from being forced to write good code, from using it.

Just because someone can pick a lock, it doesn't mean you should stop
locking your door. Now imagine a lock that forces the door to be
locked, and forces you to have the keys on you, every time you leave.
Anything that forces good habits is a good idea.
Yes, you're right. I may have been a little overzealous on my point.
At the very least const adds metadata that would let the caller know
that the method claims to not modify the state of the parameter. It's
up to the developer of that method to comply with what's advertised in
the metadata.
I think it was left out of C# because
the cost versus benefit of it tipped toward the unfavorable
direction. Implementing const reference parameters would add a
substantial amount of complexity to the language.

Really? I don't really know. It's just sad that it doesn't exist...
That doesn't necessarily mean it won't be added in the future. But, I
suspect the chances are not good for C# and even worse for VB.
>
The best way to guarantee that an object's state cannot be changed is
to make it immutable.

Ok. Something else for me to learn. Any places I can get started on
researching this? Any help is appreciated.
It's not a particularly clever concept or anything. All you do is
make sure the methods on a class have no side-effects to the object
from which they are called. The String data type is an example of an
immutable class.
Another strategy would be to use an interface or proxy object.
Though, I don't think either could absolutely be enforced at compile
time. A proxy object would probably be the best of two.

Thanks for the suggestion. This was mentioned above, and for
something where it is critical (i.e. it warrants the extra hassle
involved), it certainly could be used.
It's partially related to mutability. The difference with the proxy
object is that the real object is mutable, but the proxy is not. Like
you said, it's not something you're going to want to create for every
class.

Feb 2 '07 #18
If you need the features exposed by the C++ language and the C++ compiler so
badly then I don't understand why you are not using that instead of bleating
on about something that just "ain't gonna happen".
Stephany, This isn't a VB vs. C++ issue. It's language independent.
This is off topic.

Zytan

Feb 2 '07 #19
Brian,

Thanks again for your reply.
Yes, you're right. I may have been a little overzealous on my point.
At the very least const adds metadata that would let the caller know
that the method claims to not modify the state of the parameter. It's
up to the developer of that method to comply with what's advertised in
the metadata.
Yup, it's a godsend. I didn't realize its power until I started
making use of it. I imagine most people haven't experienced this, and
they probably think I am crazy since "if you don't want anything to
change the data, just make sure your code doesn't change it." When I
first implemented it, and it forced me to refactor about 50 functions,
some of them being major refactors, I realized it was helping me
maintain blackbox / encapsulation style programming. I was like,
"Wow." But, almost no code I see makes use of it, even professional
code...

That doesn't necessarily mean it won't be added in the future. But, I
suspect the chances are not good for C# and even worse for VB.
I imagine you are right. If no one cares... why implement it? They
have new .NET functionality to write, after all. Time is limited.

It's not a particularly clever concept or anything. All you do is
make sure the methods on a class have no side-effects to the object
from which they are called. The String data type is an example of an
immutable class.
Aaaah. I see.

Thanks for your time -- everybody -- it is appreciated.

Zytan

Feb 2 '07 #20

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

Similar topics

8
2461
by: Rickard Andersson | last post by:
What it the use of constant references in methods and functions? Is it a design issue or hwat? A reference that cannot be edited? huh? Why pass a reference then?
6
1838
by: amparikh | last post by:
I know this is something fundamental and I ought to have known it, but somehow this seems to be confusing me a lot. Fundamentally, rvalues and/or temporaries can be bound only to constant references going by the const guidelines. Taking that into consideration, how does one get a constant reference to a pointer. class A
11
5290
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from it? #include <vector> #include <string> using namespace std;
22
2130
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name* binds
6
4902
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like this: public __value enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; However, the resulting wsdl omits the actual flag values: - <s:simpleType name="Colors">
18
7326
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further processing: function saveText( scroll_list, t_area, listToBeUpdated ) { scroll_list.options.text = t_area.text; scroll_list.options.value= t_area.value; var req; var url = "http://mkmxg00/cgi/confirmUpload.pl";
13
27429
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
13
1642
by: K. Jennings | last post by:
I have a function f with the following prototype: int f(void *). From my main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ; which is fine. My question is, how can I pass the 123456 integer to f without using the intermediate x variable? Naively, what I would like to
12
1290
by: Bob Altman | last post by:
Hi all, I want to call a function that takes a void* argument and pass it the address of a constant. Is there a way to do this without explicitly defining the constant and then passing the address of the constant? In other words, what I *don't* want to do is this: const int myInt = 5; MyFunc(&myInt);
6
1780
by: Ruben | last post by:
re-reviewing the chapter in Lippan and Lajoie C++ Primer the have a section on references that I don't understand and just makes e shake my head believing that any behavior is possible with references and that they can never be understood. Maybe someone else can rephrase this so that I understand the defined behavior. cont int ival = 1024; //error: requires a const reference int *&pr_ref = &ival; //<<== this seems obvious as a...
0
8683
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
8609
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
9170
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...
1
8901
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
7739
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
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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

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.