473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why not const ref params in C#?

There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let's say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?
Nov 16 '05 #1
17 8812
I think that the basic concepts of const and ref are mutually exclusive. Do
you know of any other languages that support an equivalent to `const ref'?

I'm sure you've considered this already, but couldn't you just code your
DoSomething() not to modify HugeThing? :-)

Robert
"My Name" <ne*****@nc.rr.com> wrote in message
news:90**************************@posting.google.c om...
There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let's say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?

Nov 16 '05 #2
The reference is constant but like you say the object's state is not.

public myMethod(myObject obj)
{
obj.SomeProperty = "changed"; // changes objects state
obj = new myObject; // obj is now a local object, when method ends the passed in obj reverts back to what you passed in
}

For fun I created a readonly object. First define a class like the following:

public class ReadOnly
{
bool isReadOnly = true;

public ReadOnly()
{ }

public ReadOnly(bool initialState)
{
isReadOnly = initialState;
}

public bool IsReadOnly
{
get { return isReadOnly; }
set { isReadOnly = value; }
}
}

Now define a class that you need to make read only. This class uses the above class to make itself read only.
public class Junk()
{
private ReadOnly isReadOnly;
private string data;

// creates a read/write object
public Junk(string theData) : this(theData, new ReadOnly(false))
{ }

// create an object with a given initial state
public Junk(string theData, ReadOnly initialState)
{
isReadOnly initialState;
data = theData;
}

public string Data
{
get { return data; }
set
{
Trace.Assert(!isReadOnly.IsReadOnly, "ReadOnly Object");
data = value;
}
}
}

In your code you create an instance of a ReadOnly object then pass to the constructor of your object that you wish to make read only. Because you control this object and there is no public access to it the called method cannot make any changes to it. You can create your object as writable, make all your changes then before passing it to another method you could mark it as readonly.

For example:

ReadOnly test = new ReadOnly(true);
Junk huge = new Junk("test", test);
TestReadOnly(huge); // method that tries to change a property on huge object, will fail if it tries to change the object's state

If TestReadOnly succeeds then it didn't try to change anything. You can then change the object to writable using:
test.IsReadOnly = false;
huge.Data = "changed";

I used Trace.Assert for dramatic effect. You could use any mechanism you'd like such as throwing an exception or just ignoring the change and do nothing.

Again just for fun. It's debatable if this is a good coding practice or not but it would work to ensure nobody screws with your object's state.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.
"My Name" wrote:
There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let's say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?

Nov 16 '05 #3

"Robert Misiak" <rm*****@users.cutthispartout.sourceforge.net> wrote in message
news:DK******************@newsread1.news.pas.earth link.net...
I think that the basic concepts of const and ref are mutually exclusive. Do
you know of any other languages that support an equivalent to `const ref'? C++?

void myFunc(const BigClass &myBigClass);
A constant reference. Passes the actual object itself (as if by pointer) rather than making a copy, but it is constant. This is
one of the first things they taught me back in high school when studying recursion - how to avoid stack overflows ;)
I'm sure you've considered this already, but couldn't you just code your
DoSomething() not to modify HugeThing? :-)


What if it isn't YOUR DoSomething (aka you are writing a library that others use).

--
Adam Clauss
ca*****@tamu.edu
Nov 16 '05 #4
The point being that it is wasteful to make a copy of the object and
pass by value.


Passing an object variable by value does not make a copy of the
object, only the object reference. Make sure you read

http://www.yoda.arachsys.com/csharp/parameters.html

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
Following up on my comparison to C++...
The const reference works well there, as it maintains the low memory usage of only passing a reference, but the object (and its
members!) are still constant. In fact, the ONLY thing (AFAIK) that can be accessed when you have a const object like that are any
member functions (and maybe variables?) that are also declared const.

So:
class BigClass
{
int a;
BigClass() { a = 0;}

void CannotCallAsConst(int newA)
{
a = newA;
}

int CanCallAsConst() const
{
return a;
}

void TryToCallAsConst(int newA) const
{
a = newA; //COMPILATION ERROR
}
}

So, as you can see, everthing is still perfectly protected.

Now, apply this to C#. One big problem - methods cannot be declared const or readonly can they? In concept, it should be possible,
but the const or readonly keywords would have to be extended to apply to methods.

--
Adam Clauss
ca*****@tamu.edu

"Adam Clauss" <ca*****@tamu.edu> wrote in message news:O6**************@tk2msftngp13.phx.gbl...

"Robert Misiak" <rm*****@users.cutthispartout.sourceforge.net> wrote in message
news:DK******************@newsread1.news.pas.earth link.net...
I think that the basic concepts of const and ref are mutually exclusive. Do
you know of any other languages that support an equivalent to `const ref'?

C++?

void myFunc(const BigClass &myBigClass);
A constant reference. Passes the actual object itself (as if by pointer) rather than making a copy, but it is constant. This is
one of the first things they taught me back in high school when studying recursion - how to avoid stack overflows ;)
I'm sure you've considered this already, but couldn't you just code your
DoSomething() not to modify HugeThing? :-)


What if it isn't YOUR DoSomething (aka you are writing a library that others use).

--
Adam Clauss
ca*****@tamu.edu

Nov 16 '05 #6
I think there's some confusion here that's causing some miscommunication.

Keep in mind that C++ and C# have different sematics for passing objects as
parameters to functions -- C++ will make a temporary copy of an object if
the argument type is not a reference (&) or pointer (*). C#, on the other
hand, will pass class instances by reference -- there's no way to make C#
pass class instances by value (except when remoting is involved).

Now, the original poster posted the C# declaration:

void DoSomething(const ref HugeObject)

which, I suspect, is not what he meant, since HugeObject will be passed by
reference automatically (unless it's a struct, but we'll assume that's not
the case). So, I think that's what Robert was pointing out when he said
const and ref don't really go together. What the original poster probably
meant was simply:

void DoSomething(const HugeObject)

which would presumably indicate an object passed by reference but which the
method will not modify. This would be essentially equivalent to following in
C++:

void DoSomething(const HugeObject&)

as mentioned by Adam. There's a thred in this newsgroup from earlier today
in which John Skeet talks about some of the issues of const correctness and
gives a link to the Rotor site:

http://constcli.sscli.net/

Ken
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:O6**************@tk2msftngp13.phx.gbl...

"Robert Misiak" <rm*****@users.cutthispartout.sourceforge.net> wrote in message news:DK******************@newsread1.news.pas.earth link.net...
I think that the basic concepts of const and ref are mutually exclusive. Do you know of any other languages that support an equivalent to `const
ref'? C++?

void myFunc(const BigClass &myBigClass);
A constant reference. Passes the actual object itself (as if by pointer) rather than making a copy, but it is constant. This is one of the first things they taught me back in high school when studying recursion - how to avoid stack overflows ;)
I'm sure you've considered this already, but couldn't you just code your
DoSomething() not to modify HugeThing? :-)
What if it isn't YOUR DoSomething (aka you are writing a library that

others use).
--
Adam Clauss
ca*****@tamu.edu

Nov 16 '05 #7
Wait... yeah, after reading Mattias's post realizing I've got things mixed up again. I don't think there is a need for it in C# -
you asked about a language it was possible in, and it is possible in C++, it just is not applicable here. Sorry :)

--
Adam Clauss
ca*****@tamu.edu

"Adam Clauss" <ca*****@tamu.edu> wrote in message news:ua**************@TK2MSFTNGP11.phx.gbl...
Following up on my comparison to C++...
The const reference works well there, as it maintains the low memory usage of only passing a reference, but the object (and its
members!) are still constant. In fact, the ONLY thing (AFAIK) that can be accessed when you have a const object like that are any
member functions (and maybe variables?) that are also declared const.

So:
class BigClass
{
int a;
BigClass() { a = 0;}

void CannotCallAsConst(int newA)
{
a = newA;
}

int CanCallAsConst() const
{
return a;
}

void TryToCallAsConst(int newA) const
{
a = newA; //COMPILATION ERROR
}
}

So, as you can see, everthing is still perfectly protected.

Now, apply this to C#. One big problem - methods cannot be declared const or readonly can they? In concept, it should be possible, but the const or readonly keywords would have to be extended to apply to methods.

--
Adam Clauss
ca*****@tamu.edu

"Adam Clauss" <ca*****@tamu.edu> wrote in message news:O6**************@tk2msftngp13.phx.gbl...

"Robert Misiak" <rm*****@users.cutthispartout.sourceforge.net> wrote in message
news:DK******************@newsread1.news.pas.earth link.net...
I think that the basic concepts of const and ref are mutually exclusive. Do
you know of any other languages that support an equivalent to `const ref'?

C++?

void myFunc(const BigClass &myBigClass);
A constant reference. Passes the actual object itself (as if by pointer) rather than making a copy, but it is constant. This is one of the first things they taught me back in high school when studying recursion - how to avoid stack overflows ;)
I'm sure you've considered this already, but couldn't you just code your
DoSomething() not to modify HugeThing? :-)


What if it isn't YOUR DoSomething (aka you are writing a library that others use).

--
Adam Clauss
ca*****@tamu.edu


Nov 16 '05 #8
Yeah, I just realized that. Most of the time I understand how C# works, but sometimes I speak without thinking it through hehe :)

--
Adam Clauss
ca*****@tamu.edu

"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:On****************@TK2MSFTNGP10.phx.gbl...
I think there's some confusion here that's causing some miscommunication.

Keep in mind that C++ and C# have different sematics for passing objects as
parameters to functions -- C++ will make a temporary copy of an object if
the argument type is not a reference (&) or pointer (*). C#, on the other
hand, will pass class instances by reference -- there's no way to make C#
pass class instances by value (except when remoting is involved).

Now, the original poster posted the C# declaration:

void DoSomething(const ref HugeObject)

which, I suspect, is not what he meant, since HugeObject will be passed by
reference automatically (unless it's a struct, but we'll assume that's not
the case). So, I think that's what Robert was pointing out when he said
const and ref don't really go together. What the original poster probably
meant was simply:

void DoSomething(const HugeObject)

which would presumably indicate an object passed by reference but which the
method will not modify. This would be essentially equivalent to following in
C++:

void DoSomething(const HugeObject&)

as mentioned by Adam. There's a thred in this newsgroup from earlier today
in which John Skeet talks about some of the issues of const correctness and
gives a link to the Rotor site:

http://constcli.sscli.net/

Ken
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:O6**************@tk2msftngp13.phx.gbl...

"Robert Misiak" <rm*****@users.cutthispartout.sourceforge.net> wrote in

message
news:DK******************@newsread1.news.pas.earth link.net...
I think that the basic concepts of const and ref are mutually exclusive. Do you know of any other languages that support an equivalent to `const

ref'?
C++?

void myFunc(const BigClass &myBigClass);
A constant reference. Passes the actual object itself (as if by pointer)

rather than making a copy, but it is constant. This is
one of the first things they taught me back in high school when studying

recursion - how to avoid stack overflows ;)
I'm sure you've considered this already, but couldn't you just code your
DoSomething() not to modify HugeThing? :-)


What if it isn't YOUR DoSomething (aka you are writing a library that

others use).

--
Adam Clauss
ca*****@tamu.edu


Nov 16 '05 #9
Now that you read all that nonsense... would I ever do anything like that? Very very doubtful.

I would
1. Design my objects to be read only if indeed I didn't want some other method mucking with them. Then allow changes only through method calls that I can control.
or
2. Create some sort of lightweight property bag that I could pass to the method.
or
3. Refactoring the huge object.

That's not to say my little example is without any use. If you had say some 20-25 different object types that needed to be mindful of some main state object then doing something like I have shown below could work nicely. Instead of trying to change the overall state on 25 objects you could just change the main state object.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.
"C Addison Ritchie" wrote:
The reference is constant but like you say the object's state is not.

public myMethod(myObject obj)
{
obj.SomeProperty = "changed"; // changes objects state
obj = new myObject; // obj is now a local object, when method ends the passed in obj reverts back to what you passed in
}

For fun I created a readonly object. First define a class like the following:

public class ReadOnly
{
bool isReadOnly = true;

public ReadOnly()
{ }

public ReadOnly(bool initialState)
{
isReadOnly = initialState;
}

public bool IsReadOnly
{
get { return isReadOnly; }
set { isReadOnly = value; }
}
}

Now define a class that you need to make read only. This class uses the above class to make itself read only.
public class Junk()
{
private ReadOnly isReadOnly;
private string data;

// creates a read/write object
public Junk(string theData) : this(theData, new ReadOnly(false))
{ }

// create an object with a given initial state
public Junk(string theData, ReadOnly initialState)
{
isReadOnly initialState;
data = theData;
}

public string Data
{
get { return data; }
set
{
Trace.Assert(!isReadOnly.IsReadOnly, "ReadOnly Object");
data = value;
}
}
}

In your code you create an instance of a ReadOnly object then pass to the constructor of your object that you wish to make read only. Because you control this object and there is no public access to it the called method cannot make any changes to it. You can create your object as writable, make all your changes then before passing it to another method you could mark it as readonly.

For example:

ReadOnly test = new ReadOnly(true);
Junk huge = new Junk("test", test);
TestReadOnly(huge); // method that tries to change a property on huge object, will fail if it tries to change the object's state

If TestReadOnly succeeds then it didn't try to change anything. You can then change the object to writable using:
test.IsReadOnly = false;
huge.Data = "changed";

I used Trace.Assert for dramatic effect. You could use any mechanism you'd like such as throwing an exception or just ignoring the change and do nothing.

Again just for fun. It's debatable if this is a good coding practice or not but it would work to ensure nobody screws with your object's state.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.


Nov 16 '05 #10
In C#, *all* objects are passed by reference only, you cannot pass them by
value.
FYI, "ref" means that the reference is passed by reference which is
certainly not what you want.
There is no "const" for parameter passing in C# because it would make things
very complicates and objects should be encapsulated by themsolves making
"const" unneccesary.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"My Name" <ne*****@nc.rr.com> schrieb im Newsbeitrag
news:90**************************@posting.google.c om...
There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let's say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?

Nov 16 '05 #11
cody <pl*************************@gmx.de> wrote:
In C#, *all* objects are passed by reference only, you cannot pass them by
value.


No, objects are not passed at all. References are passed by value by
default. There's a big difference between passing a reference by value
and passing a value by reference. See
http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
> > In C#, *all* objects are passed by reference only, you cannot pass them
by
value.


No, objects are not passed at all. References are passed by value by
default. There's a big difference between passing a reference by value
and passing a value by reference.


*lol*

It's always the same..

Passing objects by reference means to pass their reference by value. You
cannot pass any Object directly as an argument, you can only pass its
reference.
Arguments are *always* passed by *value* unless the parameter is marked with
the "ref" or "out" modifier.

I know you know that Jon this was just for clearing things up for anybody :)

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #13
cody <pl*************************@gmx.de> wrote:
In C#, *all* objects are passed by reference only, you cannot pass them by value.
No, objects are not passed at all. References are passed by value by
default. There's a big difference between passing a reference by value
and passing a value by reference.


*lol*

It's always the same..


No, it's not.
Passing objects by reference means to pass their reference by value.
No it doesn't. Read some formal CS textbooks - or just the link I
posted before. Or any of the many discussions about this in the past -
I'm sure a groups.google.com search for my name and "pass by
reference" in this group will unearth plenty of threads. Pass-object-
by-reference and pass-object-reference-by-value *usually* end up having
the same effect, but not always. In particular, changing the value of
the formal parameter within the method doesn't have any effect on the
actual parameter outside the method with the latter, but would have
with the former.
You cannot pass any Object directly as an argument, you can only pass its
reference.
Yes.
Arguments are *always* passed by *value* unless the parameter is marked with
the "ref" or "out" modifier.
Yes.
I know you know that Jon this was just for clearing things up for anybody :)


I know the latter two points, but it appears that you don't quite know
the formal semantics of pass-by-reference vs pass-by-value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14
> > > > In C#, *all* objects are passed by reference only, you cannot pass
them
by
> value.

No, objects are not passed at all. References are passed by value by
default. There's a big difference between passing a reference by value
and passing a value by reference.


*lol*

It's always the same..


No, it's not.
Passing objects by reference means to pass their reference by value.


No it doesn't. Read some formal CS textbooks - or just the link I
posted before. Or any of the many discussions about this in the past -
I'm sure a groups.google.com search for my name and "pass by
reference" in this group will unearth plenty of threads. Pass-object-
by-reference and pass-object-reference-by-value *usually* end up having
the same effect, but not always. In particular, changing the value of
the formal parameter within the method doesn't have any effect on the
actual parameter outside the method with the latter, but would have
with the former.

Common, its all just termininology.

This has not very much to do with passing stuff to a method, this only has
to do with the fact that
structs have value semantics while objects haven't. If I assign a new struct
to a given struct I change its content, its value, which means if I pass a
struct to a method by reference and a I assign a new struct to it I change
the content of the original object.
Attempting to assign a new object to an object passed by reference value
doesn't change the object but only the reference variable laying on the
stack.

But this is all just syntax, at the end the only question is: is the
original object copied on the stack or is its reference copied on the stack
or is a refererence to a reference to it copied on the stack?
The first wouldn't allow any change to the passed object. The second allows
changed to the passed object while the last one allows even changes to the
variable containing the reference to the object which was passed to the
method.

1. pass by value
Foo(myStruct);

2. pass by reference
Foo(myObject);
Foo(ref MyStruct)

3. pass reference by reference
Foo(ref myObject);

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #15
cody <pl*************************@gmx.de> wrote:
Common, its all just termininology.
Yes, but it's terminology that is well-defined - and which doesn't mean
what you think it does.
This has not very much to do with passing stuff to a method, this only has
to do with the fact that
structs have value semantics while objects haven't. If I assign a new struct
to a given struct I change its content, its value, which means if I pass a
struct to a method by reference and a I assign a new struct to it I change
the content of the original object.
Attempting to assign a new object to an object passed by reference value
doesn't change the object but only the reference variable laying on the
stack.

But this is all just syntax, at the end the only question is: is the
original object copied on the stack or is its reference copied on the
stack or is a refererence to a reference to it copied on the stack?
No, that's not the only question. There's the question of the type of
the parameter - is it a reference or is it a value type, and there's
the quite separate question of whether that is passed by value or by
reference, which affects what happens if the method changes the formal
parameter.
The first wouldn't allow any change to the passed object. The second
allows changed to the passed object while the last one allows even
changes to the variable containing the reference to the object which
was passed to the method.

1. pass by value
Foo(myStruct);
Yes.
2. pass by reference
Foo(myObject);
Foo(ref MyStruct)
No - the first is *not* pass-by-reference. It's pass-reference-by-
value, which has different semantics.

In pass-by-reference semantics, a change to the formal parameter within
the method is reflected in the actual parameter. In other words, if I
call

Foo(myObject)

and Foo contains a line of code saying:

myObject = new object();

then if myObject had been passed by reference, the calling code would
then see a different value of myObject. It doesn't, because the value
of myObject (whose value is a reference to start with, not an object)
is passed by value.
3. pass reference by reference
Foo(ref myObject);


Yes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #16
> > 2. pass by reference
Foo(myObject);
Foo(ref MyStruct)
No - the first is *not* pass-by-reference. It's pass-reference-by-
value, which has different semantics.


Agreed. Theoretically we could also say that "Foo(myObject)" is passed by
value because the argument (the object reference) is -as it is- copied on
the stack.

However, I know what you mean and I agree on it and I (as a non native
english speaker) will certainly not fight with you about correct terminology
:)
In pass-by-reference semantics, a change to the formal parameter within
the method is reflected in the actual parameter. In other words, if I
call

Foo(myObject)

and Foo contains a line of code saying:

myObject = new object();

then if myObject had been passed by reference, the calling code would
then see a different value of myObject. It doesn't, because the value
of myObject (whose value is a reference to start with, not an object)
is passed by value.


I wouldn' disagree on that.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #17
cody <pl*************************@gmx.de> wrote:
2. pass by reference
Foo(myObject);
Foo(ref MyStruct)
No - the first is *not* pass-by-reference. It's pass-reference-by-
value, which has different semantics.


Agreed. Theoretically we could also say that "Foo(myObject)" is passed by
value because the argument (the object reference) is -as it is- copied on
the stack.


There's no theoretically about it :)
However, I know what you mean and I agree on it and I (as a non native
english speaker) will certainly not fight with you about correct terminology
:)


It's not a case of English - it's a case of computer science. I've seen
experienced people who know what pass-by-reference *really* means get
very confused when they get told that C# or Java pass objects by
reference. It also makes for a more confusing mental model than passing
a reference by value, as the latter is entirely consistent with the
rest of C# - assignment, type of expression, etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #18

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

Similar topics

31
by: Ben | last post by:
For many times, I've found myself changing my member variables from const back to non-const. No matter how good the original objective was, there was always at least one reason not to use const...
9
by: Gernot Frisch | last post by:
void foo(int* p) {} main() { foo( {1,2,3} ); } How can I do this? I have a class, that expects this as a constructor:
7
by: Andrej Prsa | last post by:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var) { printf ("%d\n", *var); } int main ()
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
3
by: Stan Huff | last post by:
Is there any way to disable the "params" on a particular invocation so that one can pass an array containing the arguments and not have receiver get an array having you argument array stuffed into...
8
by: David Duerrenmatt | last post by:
Hi there For some reasons, I've to use Python 1.5.2 and am looking for a workaround: In newer Python versions, I can call a function this way: func = some_function func(*params) Then,...
7
by: Mark P | last post by:
The following compiles without error on four different platforms (Linux g++, Sun CC, HP aCC, Win Dev-C++) so I suspect it's ok, but I don't see why this isn't a const-related error. pB is a...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: Giovanni Gherdovich | last post by:
Hello, I'm doing some toy experiments to see how the algoritm std::transform and the function adapter std::bind2nd can play together, but my compiler give my the error error: passing `const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.