Jump to content
  • Advertisement
Sign in to follow this  
Grain

Reference to a Reference?

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

Is this possible without venturing into unmanned code land?  Would this hypothetical method still work If I wanted a reference to a reference of a delegate instead of cows? 

namespace ContrivedExample
{
    class cow
    {
        int Weight;

        public cow(int i) { Weight = i; }

        public void HowMuchWeight()
        {
            Console.WriteLine(Weight);
        }
    }
    public class program
    {
        static void Main()
        {
            cow RefCow;
            cow Cow1 = new cow(5);
            cow Cow2 = new cow(10);
            cow ReftoRefCow;

            RefCow = Cow1;           
            ReftoRefCow = RefCow;
            ReftoRefCow.HowMuchWeight(); // Prints 5 as it should

            RefCow = Cow2;
            ReftoRefCow.HowMuchWeight(); //Still prints 5. I want to somehow make it say 10
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
You have to build a new type to do the same thing explicitly:
 
class RefToCow
{
  public cow cow;
}

...

static void Main()
{
  RefToCow RefCow;
  cow Cow1 = new cow(5);
  cow Cow2 = new cow(10);
  RefToCow RefToRefCow;

  RefCow.cow = Cow1;
  RefToRefCow = RefCow;
  ReftoRefCow.cow.HowMuchWeight();

  RefCow.cow = Cow2;
  ReftoRefCow.cow.HowMuchWeight(); // Prints 10 now.
}
This has the same behavior as doing pointer-to-pointer in C and C++. You just have to spell it all out using a member variable. Edited by Nypyren

Share this post


Link to post
Share on other sites

Ok, thanks, that worked with delegates. However not the event syntax. I had to expose a delegate reference object for each event I have. And basically the event are going unused. I'd like to be able to prevent my delegates from being nulled or invoked from the out side.

Share this post


Link to post
Share on other sites

The code is way too big to post here. 

 

I am using events for inter-object communication, but the objects don't directly know about each other so it's impossible to directly subscribe to one another's events directly. What I do instead is give groups of related objects a pool of events that they can each dump their events into and can look through and pick out ones they are interested in to subscribe to. It's in this pool that I need to keep a reference to the events. This was my initial intention at least, until I had difficulty referencing events.

 

Now I can't make your suggestion work for actual events so instead I've made each object give that pool an "Message" object that stores a delegate. 

Share this post


Link to post
Share on other sites
Is this possible without venturing into unmanned code land?

 

 

In general, the C# idiom is to make RefToRefCow a Func<RefCow>

var RefCow = Cow1;
var RefToRefCow = ()=>RefCow;
RefToRefCow().HowMuchWeight();  // 5

RefCow = Cow2;
RefToRefCow().HowMuchWeight(); // 10

Share this post


Link to post
Share on other sites

Why would you ever want to reference a reference? Why not just refer directly? I'm just curious, as I'm new to coding. I can understand it if you're trying to reference an instance rather than its parent object, though.

Edited by Malabyte

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!