Thread: Trying to understand a piece of code.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    Trying to understand a piece of code.

    I am trying to understand a piece of code. I have never seen this used before and can't seem to find any info on it. What is the purpose of the "g" and "g."? The g does not appear anywhere else in the rest of the program.

    Code:
    int main()
    {
        TicTacToe g;
        g.makeMove();
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    g is an instance of the TicTacToe class. What happens is a TicTacToe game is created, the makeMove method is called, then the game is implicitly destroyed and the program exits.

    In object oriented code like this, the engine that runs a game of tic tac toe is treated as an object, instead of having less associated functions pass around parts of the game state, or modify the global state. Here There is a TicTacToe object that you can make moves on.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, the best thing would be to lookup what classes are how to use them.
    Basically, it creates an instance of the TicTacToe class and names it g.
    Then it invokes the makeMove member function on g.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    So g doesn't necessarily need to called g, but can be called really anything? Also would using getmakeMove(); work as well?

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Yes, g is the object. It can be named whatever, so long as you refer to that same object name throughout the code.

    e.g:

    Code:
    int main()
    {
        TicTacToe wowthisisanobject;
        wowthisisanobject.makeMove();
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Brian29 View Post
    So g doesn't necessarily need to called g, but can be called really anything? Also would using getmakeMove(); work as well?
    Yes, g can be whatever; it's a local variable. In fact, it should probably be something longer, and more descriptive.

    getmakeMove would not work. makeMove is a method, not a data member. So you must call it like above.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    So something along the lines of

    Code:
    int main()
    {
        TicTacToe xogame;
        xogame.makeMove();
    
        return 0;
    }
    would work fine?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yep.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Cool, thanks for the help.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Really, again, it would be better if you took a look at how classes work instead of trying to understand piece wise functionality of the whole thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I dont understand a piece of this code
    By lilbo4231 in forum C Programming
    Replies: 25
    Last Post: 06-13-2011, 04:15 AM
  2. K&R2 - Piece of code not working.
    By reqonekt in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 11:07 AM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  5. need help to understand a piece of code
    By rraajjiibb in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 11:18 PM