Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] [C#] Pointer Alternative?

Started by EmrldDrgn Jan 29, 2007 at 4:29 PM 7 replies 4.8k views
Original Post
EmrldDrgn
EmrldDrgn
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.
Bob Janova
Bob Janova
abstract class Person { Person parent1, parent2; ...}


It's that simple. Things declared as a member of a class like that are automatically references :).
TheTroll
TheTroll
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
EmrldDrgn
EmrldDrgn
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.
RDragon1
RDragon1
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 :)
EmrldDrgn
EmrldDrgn
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.
EmrldDrgn
EmrldDrgn
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.
Nitage
Nitage
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
  • Topic Locked

    This topic has been locked by a moderator. New replies are not allowed.

    Sign in to reply to this topic.