4

If I instantiate an object like:

public class Program
{
    public static PlayerShip mainShip = new PlayerShip(); 
}

And then in another class I do:

public class RandomEncounters
{
    var subShip = new PlayerShip();

    public void PlayEncounter()
    {
        subShip = Program.mainShip;
    }
}

My understanding is that both subShip and mainShip are now referencing the same object in the 'heap' or memory. Is this correct, and also, is this a bad idea?

4
  • 1
    Why the PlayerShip subShip = new PlayerShip(); line instead of PlayerShip subShip; you are creating two instances but you are overwriting the reference to this instance with a reference to the PlayerShip of the Program class Commented Apr 14, 2017 at 10:57
  • Good point! I changed it. You are right it makes no sense to do new playership if im just overwriting the reference. Thank you! Commented Apr 14, 2017 at 11:01
  • 1
    Don't worry about heap or stack. Worry about copy-by-reference or copy-by-value; that is the relevant difference that you need to understand. Commented Apr 14, 2017 at 13:10
  • I agree with Lanorkin, it's not a bad or good idea. There are times where you would want that behavior, and there are times you don't. If you do, you can use a class, if not, you can use a struct. Commented Jul 5, 2018 at 13:42

2 Answers 2

8

My understanding is that both subShip and mainShip are now referencing the same object in the 'heap' or memory.

If PlayerShip is a class then yes, you will have two references for the same object.

If PlayerShip is a struct then no, assignment will a create a copy and will use that copy.

is this a bad idea

That's neither bad nor good, that's just a tool.

Sign up to request clarification or add additional context in comments.

Comments

0

Let's just assume that Playership is a class. Then you have different states of your execution:

public class RandomEncounters
{
    PlayerShip subShip = new PlayerShip();    //a

Now you have to different references. One reference is from the static object mainShip and the other is from the just instantiated object subShip. But after you call the method:

    public void PlayEncounter()
    {
        subShip = Program.mainShip;
    }
}

You reference to the static object mainShip, so both subShip and mainShip are referencing the same object.

is this a bad idea?

What exactly? To reference to another object? No, it's not a bad idea and it's used quite often.

But it is not a good idea to assign your object directly (I marked the line with an a) if you never use the firstly assigned object (I can't see any use, maybe you used subShip already before executing PlayEncounter(), then it would make sense again). But if you didn't use it you directly overwrite the old reference, it was just a waste of RAM space and execution time. It's not a big difference, but you can't call it a good idea either.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.