Advertisement

Period...

Started by January 01, 2003 05:34 PM
4 comments, last by Noky 21 years, 10 months ago
With the amount of time I've been here at GDev.Net you'd think I wouldn't post such a newbie question, but truth is I haven't actually done much learning. So anyway here it goes: I'm currently reading Game Programming All-In-One. I'm upto the section where I am working on a Monster clone. Not that I'm stuck but I'm wondering why do they use: Page 225 Lines: 47+
  
void CGame::ShowSplash (void)
{
m_Console->Clear ();
m_Console->OutputString ("\tWelcome to Monster1.0 \n\n");   
I don't really understand excatly what's going on here. Earlier m_Console was defined as:
  
ConLib * m_Console
   
It says according to my book that m_Console is a pointer to ConLib. That being known would that mean that Clear and OutputString are Classes or Functions (which one if I'm right) of ConLib. For reference it turns out ConLib is a Class which I have deduced from this piece of code:
  
#include "ConLib.h"

ConLib::ConLib()
{
m_Screen = GetStdHandle (STD_OUTPUT_HANDLE);
m_Keyboard = GetStdHandle (STD_INPUT_HANDLE);

SetTextColor (ConRed | ConGreen | ConBlue);
SetBackgroundColor (0);
}
   
Can I be helped, or am I doomed to never understand the underworkings of this blasted book! [edited by - PSWind on January 1, 2003 6:35:58 PM]
"That being known would that mean that Clear and OutputString are Classes or Functions (which one if I''m right) of ConLib."

Clear and OutputString are member functions of the ConLib class.

what exactly aren''t you clear on?
Advertisement
Those are functions or better yet, Methods of the ConLib object referenced by a pointer called m_Console. The reason they use -> to reference the methods is because m_Console is a pointer. When using a pointer to an object you call methods and reference variables using -> If it is just the object, then you use a period (.) to reference the methods and variables.

Examples:

ConLib m_Console; //Create a ConLib object
m_Console.Clear(); //Reference method using .

ConLib * m_pConsole; //Declare pointer to ConLib object
m_pConsole = new ConLib(); //Create a new ConLib object
m_pConsole->Clear(); //Reference method using ->

Look inside ConLib.h and you should see the definition of ConLib, including the functions you refered to.
One of the primary reasons for having classes, is so that you can group data and functions together. Clear and OutputString are called "member functions" in C++ lingo, and "methods" in object-oriented lingo.

If m_Console were not a pointer to a class, but an actual instance of a class, the syntax would be sightly different.

It could be done this way:
ConLib m_Console;
m_Console.Clear();
m_Console.OutputString("/nMonster 1.0/n/n/");


How it''s defined now:
ConLib* m_Console = new ConLib;

Because m_Console is a pointer, we need to deference it before we can use it. * is the deferencing operator:
*m_Console

Then we use . to index the structure and pick which function (or data) we want to work with).

*m_Console.Clear();

However that code has a problem. The . operator has higher precedence than the * operator does. Much like how multiplication is performed prior to addition. To fix either case, we add a pair of ().

(*m_Console).Clear();

This is very clumsy, and often used, so K&R (the guys who designed C) added a shortcut, the -> operator.

m_Console->Clear();

That''s the same thing as the last line.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Oh okay so because it''s a pointer to the class we use a -> where as if it was the class itself we would use a "."

This topic is closed to new replies.

Advertisement