Well, in C++ it is appropriate to use pointers, C# kindof does it
automatically.
That is, the C++ syntax for the following is very similar to the
corresponding C# syntax:
(C++) MyClass *p = new MyClass ();
(C#) MyClass p = new MyClass ();
In both cases, they are both heap-based objects, and p holds a reference to
that object on the heap.
NOW, I need a C++ guru to help me out (I believe that Method1 from C++ is
pretty much the same as Method1 from C#, and the same for Method2. Anybody
care to comment?)
There are two ways to pass the above variable p in C++:
Method1: (pass a pointer, by value)
(C++) WorkWithObject(MyClass *myObject) { myObject->ClassMethod(); }
(C++) WorkWithObject(p);
Method2: (pass by reference)
(C++) WorkWithObject(MyClass &myObject) { myObject.ClassMethod(); }
(C++) WorkWithObject(*p);
Which are very similar (I believe) to the following two C# methods:
Method1: (pass by value)
(C#) WorkWithObject(MyClass myObject) { myObject.ClassMethod(); }
(C#) WorkWithObject(p);
Method2: (pass by reference)
(C#) WorkWithObject(ref MyClass myObject) { myObject = new MyClass() }
(C#) WorkWithObject(ref p);
I don't think these are 100% the same. It's funny that I believe in C++,
the pass by reference is often preferred. In C#, pass by value is FAR
preferred (to the extent that using ref is strongly discouraged, unless you
REALLY need it).