Tearing Hair Out Over C++ Conversion

P

PseudoBill

I'm trying to convert some Managed C++ code to C# and I'm having
trouble figuring out what can be converted to regular C#-style
references (i.e., just dropping the "*" or "&" and replacing
"->" with ".") and what is truly 'unsafe' code (i.e., where I
should leave the "*", "&", "->" as they are and mark the block
unsafe).

What would the C# version of the following 4 declarations and method
calls be?

//a lot of String declarations done this way:
SomeValueType *x1;
x1->Method1();

SomeNonValueType *x2 = new SomeNonValueType();
x2->Method2();

SomeValueType &x3;
x3.Method3();

SomeNonValueType &x4 = *new SomeNonValueType();
x4.Method4();

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
S

Scot McDermid

You should be able to find this in a C# reference book.
But... I'll see what if I can help.

By the way, I'm new at this C# stuff...

PseudoBill said:
I'm trying to convert some Managed C++ code to C# and I'm having
trouble figuring out what can be converted to regular C#-style
references (i.e., just dropping the "*" or "&" and replacing
"->" with ".") and what is truly 'unsafe' code (i.e., where I
should leave the "*", "&", "->" as they are and mark the block
unsafe).

What would the C# version of the following 4 declarations and method
calls be?
//a lot of String declarations done this way:
SomeValueType *x1;
x1->Method1();

If it truly is a value type then the declaration and call would be
SomeValueType x1; // declares the x1 variable
x1.Method1(); // calls the method

In C#, strings are reference types.
string s1; // declare
s1 = "abc"; // set
string s2 = "def"; // combining the two is fine
s1.Method(); // there are some built in methods for strings
SomeNonValueType *x2 = new SomeNonValueType();
x2->Method2();

I take in that *x2 is a pointer to the newly created object.
In C# you don't need the * and you use .
SomeNonValueType x2 = new SomeNonValueType();
x2.Method2();
SomeValueType &x3;
x3.Method3();

Is Method3 a "static" method? In a C# "static" method you
don't actually need to declare an instance. You can just go
ahead and call the Method. General Example:
ClassName.MethodName(); // just call the "static" method

Concrete Example:
System.Console.WriteLine("Hello World."); // no need to have a pointer
to "console"

I don't know C++ so I'm only guessing what the * and &
are doing in your examples above.
SomeNonValueType &x4 = *new SomeNonValueType();
x4.Method4();

Again... I don't know what the & and * are getting at.
I'm sure I must be missing something.

SomeNonValueType x4 = new SomeNonValueType(); // set x4 to the newly
created object.
x4.Method4(); // call the method

The simplistic answers above are all considered
safe code. x1, x2, x3, x4 are "references" not pointers.
And, as such you can't do pointer arithmetic

As far as unsafe code, C# considers any pointer
arithmetic to be unsafe. MS C# Core reference
also lists the following as "unsafe"
- using platform invoke (P/Invoke) to interoperate
with existing COM or Win32 code, and you have
data structures with embedded pointers.
- if you are dealing with legacy data structures that are
written directly to a disk or a socket stream.
 
P

PseudoBill

x1 is a pointer to a value type.
x2 is a pointer to a non-value type.
x3 is a reference to a value type.
x4 is a reference to a non-value type.

If the declaration "SomeType *y;" becomes "SomeType y" in C#, then
what about the declaration "SomeType &z" ? Does this also become
"SomeType z" ? Even though y is a pointer and z is a reference?

I'm willing to leave some as "unsafe" - I just want the actual C#
equivalent.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
S

Stefan Simek

All the reference stuff in C++ is just a shortcut to avoid writing lots of
*'s in my opinion. In the C# world, you don't need to use any of them.

You can't declare a pointer to a value type (outside of unsafe context),
only pass a value type by reference to a method, like:

void ProcessSomeValueType(ref SomeValueType val)
{
...
}

As for the x2 and x4, it's the most common case in C#, both of them are

SomeNonValueType x = new SomeNonValueType();
x.Method();

As for the x3, you can't have a reference to value type except when passing
as an argument to a method (see x1).

HTH,
Stefan
 
S

Scot McDermid

PseudoBill said:
x1 is a pointer to a value type.

No such thing as a pointer to a value type in C# (outside of
an unsafe context).
x2 is a pointer to a non-value type.

"Pointers" are called "references" in C#. "Pointer"
seems to be considered a dirty word.
x3 is a reference to a value type.
x4 is a reference to a non-value type.

huh? What's the difference between a pointer and
a reference? I'm gonna guess that C++ lets you do
pointer arithmetic with pointers but won't let you
use arithmetic with "references". If that is the case
then C# reference type variables are essentially the same
thing as C++ references but leave off the "&".

C# example:
object o1 = new object(); // declares and allocates an object
object o2; // declares a "reference" but doesn't allocate an object
object o3; // declare one more

o2 = o1; // point o2 to the same place that o1 points
o3 = o1.Clone(); // Clone method creates a shallow copy.

So after the code runs there are two objects on the heap.
One object is referenced by BOTH o1 and o2 "references". And the other
object is referenced by the o3 "reference".

In his post Stephen brings up an important point about parameter
passing. In C#, if you want to pass by reference you need the ref
keyword in both the method declaration AND the call to the method.

class MyClass {
public void Method(ref int i) // ref keyword for the parameter
{ ... }
}

MyClass o = new MyClass();
int mylocalvariable;
....
o.Method(ref mylocalvariable); // need the ref keyword for the call
 
J

Jeff Louie

PseudoBill... Of course C# does not have deterministic destructors or
automatic garbage collection. In general objects in C++ (on the stack)
use value semantics. In C# objects use only reference semantics. The
closest thing to objects on the stack in C# is a struct which is
generally used to define your own type such as BigMoney. So objects in
C# use reference semantics and structs in C# use value semantics.

int main()
{
// TODO: Please replace the sample code below with your own.
int i;
cout << "Hello World" << endl;
Test t(1);
Test *p = new Test(2);
delete p;
//Test &rv(3); // won't compile
Test &rr= *new Test(4);
delete &rr;
cin >> i;
return 0;
}
SomeValueType *x1;
x1->Method1();<
A pointer to an object on the stack. The closest is a struct in C# ->
SomeStruct s= new SomeStruct();
SomeNonValueType *x2 = new SomeNonValueType();
x2->Method2();<
A pointer to an object on the heap. delete x2; The closest is a ref
variable in C# -> SomeObj o= new SomeObject();
SomeValueType &x3;
x3.Method3();<
A ref to an object on the stack. I cannot get this to compile, although
I can create an independent ref.
SomeNonValueType &x4 = *new SomeNonValueType();
x4.Method4();<
A reference to an object on the heap. delete &x4; The closest is a ref
variable in C# -> SomeObj o= new SomeObject();

Regards,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top