Jump to content
  • Advertisement
Sign in to follow this  
EmrldDrgn

[.net] [C#] Pointer Alternative?

This topic is 4312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
abstract class Person {
Person parent1, parent2;
...
}


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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    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 :)

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!