[.net] [C#] Pointer Alternative?

Started by
7 comments, last by GameDev.net 17 years, 2 months ago
I have an abstract Person base class that has derived classes. I want each Person to have parents, which I was trying to implement by putting two Person* in the class, so I could still find out if they were doctors or lawyers or any other derived Person class. However, MSVC# '05 EE is complaining saying pointers can only be used in an "unsafe environment"? What does this mean, and how do I avoid it? I know I can just put "unsafe" before them and compile with /unsafe, but that seems like I'm basically telling the compiler to shut up, which is not good. Advice?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Advertisement
abstract class Person { Person parent1, parent2; ...}


It's that simple. Things declared as a member of a class like that are automatically references :).
C# and variables...

C# deal with variables one of two ways depending on what they are. Intergral type (int, short, ect) and struct are dealt with by value, classes are dealt with by reference. That means that although we don't see it when you do the following; Parent father = new Person(), father is really a reference to the new Person you created. If you pass father to a function you are passing a reference to father, so the function with be able to change father, just like as if it was a pointer to father. This lil fact really messed me up when I first started using C#.

theTroll
I can do that even though you can't instantiate an abstract class? I can say
Person Smith = new Doctor();

?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Quote:Original post by EmrldDrgn
I can do that even though you can't instantiate an abstract class? I can say
Person Smith = new Doctor();

?


Try it :)
Sweet. It works. I don't see much of a rationale for handling things that way, other than the "it's simpler" explaination (which, now that I think of it, is all the rational you really need).

Thanks for all your help, everyone!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Sweet. It works. I don't see much of a rationale for handling things that way, other than the "it's simpler" explaination (which, now that I think of it, is all the rational you really need).

Thanks for all your help, everyone!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
The terminology can be confusing when you're used to C/C++.

In C# the following is true of references:
  • When a variable of a class type is declared, it is automatically a reference

  • A C# reference can be null (unlike a C++ reference)

  • A C# reference can be used polymorphically (that is, a C# reference can refer to classes derived from it's declared type)

  • C# references are garbage collected
  • And to answer your questions about pointers, pointers are unsafe, so if you think of something like

    int[] i = new int[4];
    fixed( int *ip = i )
    {
    ip[12] = 0;
    }

    this will be bad, the compiler make sure you know what you're doing and that you're wanting this behaviour and any poopups is your fault :)

    This topic is closed to new replies.

    Advertisement