469,592 Members | 1,808 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

How to pass a const struct by reference

I have a huge struct with many members.I don't want to copy
this struct to the stack, that's why i pass it by reference to
a function. But also i want the function to be unable to modify it.
Something like that in C++:

bool func( const Str& myStr )
{
....
}

Nov 17 '05 #1
4 3052
I don't know if this is want you want, but you can box a structure. int
maps to struct Int32.

int i= 1; // value semantics
object o= i; // reference semantics

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
An immutable object may be an appropriate solution.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Alexander Kolev <Al************@discussions.microsoft.com> wrote:
I have a huge struct with many members.I don't want to copy
this struct to the stack, that's why i pass it by reference to
a function. But also i want the function to be unable to modify it.
Something like that in C++:

bool func( const Str& myStr )
{
....
}


I'd first check whether you really need it to be a struct - is there
any particular reason for it being a value type in the first place?
Usually things with lots of members aren't good candidates for being
value types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Alexander... I was too busy to give any lengthy answer earlier. There is
no
equivalent of that use of C++ const in C#. The general solution is store
the
data in a read write collection and then wrap a reference to the
collection in a
read only class using containment by reference. You can then pass a
reference to the read only class to the client. In this manner only one
copy of
the collection exists. A smarty solution is to derive a read write class
from a
read only class, create an instance of the read write class and pass a
reference to the read only base class. This is not foolproof since the
client can
still cast the reference to the actual read write class much as you can
const_cast in C++.

In C++ both struct and classes have value semantics. In C# structs have
value
semantics and classes have reference semantics. If you store a struct in
most
C# collections they get boxed anyway, so in general, classes are favored
over
structs.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Virendra Verma | last post: by
41 posts views Thread by Berk Birand | last post: by
7 posts views Thread by ritchie | last post: by
7 posts views Thread by Mark P | last post: by
14 posts views Thread by Abhi | last post: by
3 posts views Thread by VirGin | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.