Jump to content
  • Advertisement
Sign in to follow this  
Stormtrooper

[C#] Pointers?

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

In my game I have a ResourceManager class. If I pass an instance of this class to another class...is a copy created or does it pass a pointer? For example, say I have a test class that simply holds a string. I create an instance of the class, then pass it in the constructor of another class. If I modify the string of the class, will it change everywhere? I'm assuming it would since I'm assuming the instance and not creating a new instance.

Share this post


Link to post
Share on other sites
Advertisement
classes pass by reference.
structs pass by value (copy).

strings pass by reference, but you can't modify them in place (all of the operations return a new (modified) string; leaving the original in place).

Share this post


Link to post
Share on other sites
I thought that in order to pass by reference in C# you had to use the ref keyword.

[source lang = "C#"]
class Bar
{
public int SomeInt;
public Bar(int someint)
{
SomeInt = someint;
} //end f()
} //end class

class Foo
{
static void DoSomethingByVal(Bar x)
{
x.SomeInt += 5;
} //end f()
static void DoSomethingByRef(ref Bar x)
{
x.SomeInt += 5;
} //end f()

static void Main()
{
Bar bar1 = new Bar(10);
Bar bar2 = new Bar(10);
DoSomethingByVal(bar1); //bar1.SomeInt still == 10: NOT TRUE
DoSomethingByRef(bar2); //bar2.SomeInt now == 15
} //end f()
} //end class




I decided to run this code before posting it and it turns out that in both cases SomeInt was increased by five. So, clearly what Telastyn said was true but what am I missing here. Why bother with the ref keyword if classes are passed by reference anyhow (or is it just for structs) and is there any to pass classes by value?

Share this post


Link to post
Share on other sites
I'm sure those articles cover the details, but just so everyone understands the core situations, they are:

1. Passing Value Type By Value (struct without ref keyword)
2. Passing Reference Type By Value (class without ref keyword)
3. Passing Value Type By Reference (struct with ref keyword)
4. Passing Reference Type By Reference (class with ref keyword).

1.

int Clamp(int x)
{
if(x > MAX_VALUE)
x = MAX_VALUE;
return x;
}

returns x or MAX_VALUE, but does NOT change the value in the caller
changes x to MAX_VALUE if needed (affects caller)

3.

void Clamp(ref int x)
{
if(x > MAX_VALUE)
x = MAX_VALUE;
}

2.

void DisableControl(Control control)
{
control.Enabled = false;
}


disables the control that the caller passing in (affects caller)

4.

void CreateControl(ref Control control)
{
control = new Control();
}

changes the caller to have a different control instance.
----
ASSIGNMENT is the ONLY reason you would ever need to pass a reference type by reference ... this is 100% equal to passing a pointer to a pointer in C.

Case 2 and 3 are equal to each other as well, identical to a normal pointer in C.

NOTE: edited order according to ezbez's comment.

[Edited by - Xai on August 20, 2008 11:09:08 PM]

Share this post


Link to post
Share on other sites
Xai, your examples aren't consistently numbered with your original points. I think they should be 1, 3, 2, 4, respectively.

Share this post


Link to post
Share on other sites
Ok, thanks for the help. This is the reason I ask, maby there is a better way to do this. I have a ResourceManager class that simply holds a List<> of all the resources(textures, fonts, etc.). The controls in the GUI, entities, etc need to have a texture. The problem is releasing and reloading the textures in the DirectX device is lost and reset. Either I can pass the Resource Manager, or pass a reference of the texture to the GUI. Instead of having to go through every single control, I can simply have a method in the Resource Manager that releases and reloads all the textures...correct?

Share this post


Link to post
Share on other sites
Something interesting I found is that you can't use polymorphism with references. Is there around this?

Label : Control
Container.AddControl(Label)

cannot convert from 'BrokenSilence.Gui.Controls.Label' to 'ref BrokenSilence.Gui.Base.Control'

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
2. Passing Reference Type By Value (class without ref keyword)

Isn't this in contradiction with what Telastyn said?

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!