-3

I'm at an intermediate level when it comes to programming and I came across something that I have yet to learn about.

This code is for a basic Tetris game and it is from this web page: http://javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

This is his header file that defines the class Pieces:

#ifndef _PIECES_
#define _PIECES_

class Pieces
{
 public:

    int GetBlockType        (int pPiece, int pRotation, int pX, int pY);
    int GetXInitialPosition (int pPiece, int pRotation);
    int GetYInitialPosition (int pPiece, int pRotation);
};

#endif // _PIECES_

Now, in his next header file, for a class called Board, he creates a constructor prototype which takes Pieces *pPieces as an argument. You can see as follows:

class Board
{
 public:

     Board                       (Pieces *pPieces, int pScreenHeight);

     int GetXPosInPixels         (int pPos);
     int GetYPosInPixels         (int pPos);
     bool IsFreeBlock            (int pX, int pY);
     bool IsPossibleMovement     (int pX, int pY, int pPiece, int pRotation);
     void StorePiece             (int pX, int pY, int pPiece, int pRotation);
     void DeletePossibleLines    ();
     bool IsGameOver             ();

 private:

     enum { POS_FREE, POS_FILLED };                   
     int mBoard [BOARD_WIDTH][BOARD_HEIGHT]; // Board that contains the pieces
     Pieces *mPieces;
     int mScreenHeight;

     void InitBoard();
     void DeleteLine (int pY);
};

     #endif // _BOARD_

I have never come across something like this before. My question is what is this declaration of *pPieces that seemingly uses the class name Pieces as a data type? Can someone explain to me the reason for it? Or what it achieves? Or what this is called so I can read more about it?

longAD
  • 53
  • 1
  • 6
  • 6
    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – juanchopanza Aug 04 '14 at 18:45
  • 3
    A class is both a module and a data type. This is fundamental to how C++ works. – Keith Thompson Aug 04 '14 at 18:46
  • 2
    Go and read about classes, structures and pointers. – masoud Aug 04 '14 at 18:48
  • 5
    `I'm at an intermediate level when it comes to programming` Please understand that I am not trying to be rude, just trying to help you evaluate yourself better. You are far from `intermediate level` if you don’t know about pointers or that classes are user-defined data types. – bolov Aug 04 '14 at 18:50
  • @juanchopanza: considering that *the highest voted entry* before the list was vandalized and locked by mod George Stocker, was "I'd add [C++ FAQs](http://rads.stackoverflow.com/amzn/click/0201309831) to the Beginner list.", do we really want to refer to that list. yesterday or was it day before i had to apologize, sorry, for doing so, because the information that i wanted the reader to check was just incorrect in Stocker's version. i fixed that particular, but now today it's so again: the info that you want the OP to get aware of would include that highest voted entry that Stocker deleted. – Cheers and hth. - Alf Aug 04 '14 at 18:53
  • 3
    @Cheersandhth.-Alf: The controversy there was that the [online FAQ](http://www.parashift.com/c++-faq/) is not a book, which is what the question specifically asked for. Your link is to an actual paper book, which has much of the same content as the web site. I don't think the distinction matters for this question. But I'd say that a tutorial would be more appropriate than an FAQ; no offense, but someone who doesn't know that a class is a data type likely doesn't yet know what questions to ask. – Keith Thompson Aug 04 '14 at 19:00
  • 1
    @KeithThompson: sorry i was not aware of any controversy. but folks focusing on whether an entry conformed to the literal wording of the artifical question (which could just be changed if necessary), and for an entry which does have a dead tree version. jeez. that must have been politicians. – Cheers and hth. - Alf Aug 04 '14 at 19:17
  • @bolov: wow, you'd do that for me? Alright, thanks guys, I'll read up on classes and pointers once more and see what I missed out. – longAD Aug 04 '14 at 19:23

1 Answers1

2
Pieces *pPieces

pPieces is a pointer to an object. That object has a type. That type is a user-defined type which is declared by a class named Pieces. In fact Pieces has been declared as a new type in the code:

class Pieces
{
   // ...
};
masoud
  • 55,379
  • 16
  • 141
  • 208