Note that there are some explanatory texts on larger screens.

plurals
  1. POClass Composition in Objective-C Question: Is it possible to inherit a class variable?
    primarykey
    data
    text
    <p>To help gather a sense of Objective-C I'm creating a <em>very</em> basic connect 4 game sans Cocoa.</p> <p>In my program I have three modules:</p> <ul> <li>"Game": Contains an array that holds the board information. Object that is created within main, and is responsible for turns. Player and Computer objects live within this module.</li> <li>"Player": Holds a function that inserts a player's piece into the preferred column (this module exists for the purpose of encapsulation, and only that).</li> <li>"Computer": Holds functions that determine, based on current board setup, where the computer should move, and then places a piece in that location.</li> </ul> <p>Ideally I would like for the Player and Computer classes to be able to edit the same instance of <code>pieceLoc</code> that lives within Game through some sort of inheritance, however I am not sure how to do this.</p> <p>Here's a snippet of what I currently am thinking about:<br /> <code> // startGame exists within the "Game" module</p> <pre><code>char *pieceLoc[42]; // This is a *temporary* global var. I'm still learning about // the best way to implement this with class and instance // methods. This is the array that holds the board info. -(void) startGame { Player *player =[[Player alloc] init]; // player's symbol = 'X' Computer *computer =[[Computer alloc] init]; // computer's symbol = 'O' int i = 0; while ([game hasPieceWon: 'X'] == NO &amp;&amp; [game hasPieceWon: 'O'] == NO) { //function for player move if ([game hasPieceWon: 'X'] == NO) { //function for computer move } } if ([game hasPieceWon: 'X'] == YES) // Tell the player they've won else // Tell the player the computer has won. </code></pre> <p>} </code> <br /><br /></p> <p>The functions used for player and computer moves are the functions that would need to somehow obtain access to the array <code>pieceLoc</code> (which will in the future exist as a instance variable, once I learn more about class vs. instance methods). <code>pieceLoc</code> currently exists as a char *, in case I do have to pass it via function parameters.</p> <p>I feel as if this is a fairly simple problem with how I'm thinking about OOP, but I was unable to find an answer to what I was looking for despite searching for most of the afternoon. From what I've gathered my question relates to class composition, but I couldn't find a good resource about that for Objective-C.</p> <p>So, again: I am looking for a way to pass a single instance of <code>pieceLoc</code> that lives within "the parent class" Game to two "children classes" where <code>pieceLoc</code> can be directly manipulated <em>without</em> using additional parameters.</p> <p>If passing the array as a parameter does end up being the more idiomatic thing to do, would I be able to get an example of how passing by reference works in Objective-C?</p> <p>Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload