Jump to content
  • Advertisement
Sign in to follow this  
deej21

object references

This topic is 4196 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

Hello, I have a question about object references, and I am on vacation without a compiler to check it, but still programming (notepad ftw.) I'm using C#. There are four classes, Base, Sub1 : Base, Sub2 : Base and a main class that has a Hashtable with keys of strings and values of Base. The Sub1 class will sometimes call "MainClass.foo(this);" where foo has a parameter of Base. Now, the main class has a reference to said Sub1 in its Hashtable, and also now passed as a parameter. These reference the same object. What if foo has code: paramBase = new Sub2(); Will this change the reference in the Hashtable to now point to the same thing? Part of me wants to say yes, yet that command merely changes paramBase to point to something else; wouldn't the Sub1 still exist in memory, still referenced in the Hashtable? If indeed the Hashtable's reference is also changed to the new Sub2, then the Sub1 is effectively gone, waiting for the garbage collector, correct?

Share this post


Link to post
Share on other sites
Advertisement
I think I understand your problem.

If you change one reference, you don't change any other reference (even if they originally pointed to the same object). So if you change the reference in the class, but not the hashtable, the hashtable reference will still point to the old instance.

Share this post


Link to post
Share on other sites
I'm quite confident that it will only change the local reference. The hashtable should continue pointing to what it had been previously. What you need is some form of double reference. In C++, what I'm most familiar with, you'd have a whole bunch of options, an iterator to a pointer, a pointer to a pointer, reference to a pointer, iterator to a smart pointer, et cetera. I'm not sure what the equivalent in C# would be. I'm actually rather surprised I haven't had to figure this out for myself yet. I suppose you could just create a new class, and all the class did was contain a reference to another class. (You could even make it a generic class.) And then you store this reference class in the hashtable, and this reference class stores a reference to Base. But that feels like it would just be reproducing some effect that should already exist.

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!