3

I stripped my code to the parts that cause the problem. The code jumps back and forth lines 5 and 9 here, causing stackoverflowexception.

How could I do this differently? I need the Platform instance inside Game class to use in functions.

namespace Games
{
    public class Game
    {
        private Platform platform = new Platform();
    }
    class Platform : Game
    {
        private bool[] squares = new bool[9];
    }
}
3
  • 2
    Don't make Platform a sub-class of Game. Commented Nov 18, 2018 at 13:23
  • Finally. That fixed the issue, thank you so much! This is for a school assignment and I think I read the class diagram wrong. Commented Nov 18, 2018 at 13:34
  • +n; in my opinion, this is the perfect question for stackoverflow. Commented Nov 18, 2018 at 13:37

1 Answer 1

1

When a Game instance is created it creates an instance of Platform which will invoke the base class constructor which creates an instance of Platform which will invoke the base class constructor which will...

See where this is leading?

You should use Platform where you are trying to use Game. Many would argue not to use inheritence at all. Consider composition which in your case might mean Game has a property of type Platform but Platform does not inherit from Game.

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

1 Comment

That makes sense now. I tried to solve this badly and figured something like this was happening, but that's so nicely explained. Thank you sir.

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.