- Thanks received
- 114
Very interesting SC - nice one!
Surely you mean just passing a reference to an object, and not the object itself?
Surely, Shirley?
[FONT="Courier New"][SIZE="2"][COLOR="blue"]namespace[/COLOR] DontChangeTheStateOfReferenceObjectsInPublicMethods
{
[COLOR="blue"]class[/COLOR] [COLOR="deepskyblue"]MyClass[/COLOR]
{
[COLOR="blue"]public int[/COLOR] SomeIntProperty { [COLOR="blue"]get[/COLOR]; [COLOR="blue"]set[/COLOR]; }
}
[COLOR="blue"]static class[/COLOR] [COLOR="deepskyblue"]MyStaticClass[/COLOR]
{
[COLOR="blue"]public static void[/COLOR] MangleMyClass([COLOR="blue"][highlight]ref[/highlight][/COLOR] [COLOR="deepskyblue"]MyClass[/COLOR] someClassToBeMangled)
{
someClassToBeMangled.SomeIntProperty = 100;
[COLOR="seagreen"]//I don't care what it was when you passed this object in[/COLOR]
}
}
[COLOR="blue"]public class[/COLOR] [COLOR="deepskyblue"]DoStuff[/COLOR]
{
[COLOR="deepskyblue"]MyClass[/COLOR] myInstanceOfMyClass = [COLOR="blue"]new[/COLOR] [COLOR="deepskyblue"]MyClass[/COLOR] { SomeIntProperty = 900 }; [COLOR="seagreen"]
//I really want this to still be '900' later![/COLOR]
[COLOR="blue"]public[/COLOR] DoStuff()
{
[COLOR="deepskyblue"]MyStaticClass[/COLOR].MangleMyClass([COLOR="blue"][highlight]ref[/highlight][/COLOR] myInstanceOfMyClass);
[COLOR="seagreen"]/* myInstanceOfMyClass will be 100 at this point, and I wont
be sure why if I don't have access to the code for MyStaticClass.MangleMyClass */[/COLOR]
}
}
}[/SIZE][/FONT]
void mangle( MyClass& objectToBeMangled )
{
}
void someOtherFunction( const MyClass& objectNotToBeManged )
{
mangle( objectNotToBeMangled ); // compile error : cannot convert const MyClass& to MyClass&
}

Code:void mangle( MyClass& objectToBeMangled ) { } void someOtherFunction( const MyClass& objectNotToBeManged ) { mangle( objectNotToBeMangled ); // compile error : cannot convert const MyClass& to MyClass& }
That's how the professionals do it.
Seriously I find it utterly incomprehensible that the forefathers of Java and C# didn't carry this over from C++, as it makes such a difference to the reliability and maintainability of software when you use const types properly.
C# has structs that achieve much the same effect; if 'MyClass' were defined as a struct in my example above (which you could do simply by changing the class keyword to struct), then whether you used the ref keyword or not would affect the outcome.
However, there's a trade-off in terms of memory usage since structs are stored on the Stack rather than on the Managed Heap, and so aren't suitable for .net's Garbage Collection mechanism. In order to benefit from Garbage Collection, you need to rely on developers using Reference Types sensibly.
That's not the same effect. You're talking about pass by value, which means a potentially expensive memory copy just to avoid the scenario where the function might change the original data, and wouldn't stop the programmer trying to change the data and spending ages debugging why it wasn't working.
In C++ you pass a constant reference to the object, which means you have limited read-only access to the original object (functions that are marked const), but it's still the original object. Trying to change the object results in a compile error.
The OP's rant was obviously down to spending ages trying to find a bug introduced by some junior programmer. If it had been done in C++, the bug would never have existed because the code wouldn't have compiled.
Why o why are people still doing this and who is teaching these progammers that they are allowed to do this?
Easily the fastest route to a tulipe codebase.
Does my fuggin head in.
</rantOver>