<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'A VERY basic question about classes' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'A VERY basic question about classes' posted on the 'Beginner C/C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 00:35:29 -0700</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 00:35:29 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>A VERY basic question about classes</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/410197/410197/a-very-basic-question-about-classes/</link>
      <description>This is Windows API code, but this is such a basic question about classes I'm posting it here. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What I am confused about concerns class definition and usage. &lt;br /&gt;
In the following class definition:&lt;br /&gt;
WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst); &lt;br /&gt;
If I change this to:&lt;br /&gt;
WinClass (WNDPROC, char const *, HINSTANCE);&lt;br /&gt;
The program still runs fine, so my question is what exactly is the significance of "wndProc" in "WNDPROC wndProc" to the compiler? Is this only for notation/readability purposes? Or does wndProc have any actually meaning apart from giving some clue as to what to pass WNDPROC?&lt;br /&gt;
&lt;br /&gt;
Thank you for any responses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Header:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
#if !defined WINNIE_H
#define WINNIE_H
//------------------------------------
//  winnie.h
//  (c) Bartosz Milewski, 1995, 97
//------------------------------------

#include &amp;lt;windows.h&amp;gt;

// Forward declaration of our Window Procedure
LRESULT CALLBACK WindowProcedure
    (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

// We'll be creating windows of this Class in our program
class WinClass
{
public:
    WinClass (WNDPROC, char const *, HINSTANCE); //(WNDPROC wndProc, char const * className, HINSTANCE hInst);
    void Register ()
    {
        ::RegisterClass (&amp;amp;_class);
    }
private:
    WNDCLASS _class;
};

// Creates a window of a given Class
class WinMaker
{
public:
    WinMaker (): _hwnd (0) {}
    WinMaker (WNDPROC, char const *, HINSTANCE); //(char const * caption, char const * className, HINSTANCE hInstance);
    void Show (int cmdShow)
    {
        ::ShowWindow (_hwnd, cmdShow);
        ::UpdateWindow (_hwnd);
    }
protected:
    HWND _hwnd;
};

#endif

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
source file:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
//------------------------------------
//  winnie.cpp
//  (c) Bartosz Milewski, 1995
//------------------------------------

#include "winnie.h"

// This is the entry point of every Windows program
int WINAPI WinMain
   (HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    char className [] = "Winnie";
	
	// Define a Window Class and register it under the name "Winnie"
	WinClass winClass (WindowProcedure, className, hInst);
    winClass.Register ();

	// Create and show a window
    WinMaker win ("Hello Windows!", className, hInst);
    win.Show (cmdShow);

    MSG  msg;
    int status;
	// Keep pumping messages--they end up in our Window Procedure
    while ((status = ::GetMessage (&amp;amp;msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        ::DispatchMessage (&amp;amp;msg);
    }

    return msg.wParam;
}

WinClass::WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst)
{
    _class.style = 0;
    _class.lpfnWndProc = wndProc;  // Window Procedure: mandatory
    _class.cbClsExtra = 0;
    _class.cbWndExtra = 0;
    _class.hInstance = hInst;           // owner of the class: mandatory
    _class.hIcon = 0;
    _class.hCursor = ::LoadCursor (0, IDC_ARROW); // optional
    _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // optional
    _class.lpszMenuName = 0;
    _class.lpszClassName = className;   // mandatory
}

WinMaker::WinMaker
    (char const * caption, char const * className, HINSTANCE hInstance)
{
    _hwnd = ::CreateWindow (
        className,              // name of a registered window class
        caption,                // window caption
        WS_OVERLAPPEDWINDOW,    // window style
        CW_USEDEFAULT,          // x position
        CW_USEDEFAULT,          // y position
        CW_USEDEFAULT,          // witdh
        CW_USEDEFAULT,          // height
        0,                      // handle to parent window
        0,                      // handle to menu
        hInstance,              // application instance
        0 );                    // window creation data
}

// Window Procedure called by Windows with all kinds of messages

LRESULT CALLBACK WindowProcedure 
    (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
		// In this simple program, this is the only message we are processing
        case WM_DESTROY:
			::PostQuitMessage (0);
            return 0; // return zero when processed

    }
	// All the unprocessed messages go there, to be dealt in some default way
    return ::DefWindowProc (hwnd, message, wParam, lParam );
}


&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/410197/410197/a-very-basic-question-about-classes/</guid>
      <pubDate>Wed, 02 Dec 2009 12:35:12 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: A VERY basic question about classes</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/410197/410204/re-a-very-basic-question-about-classes/#410204</link>
      <description>I have another question about this code. &lt;br /&gt;
&lt;pre class="sourcecode"&gt;WinMaker (): _hwnd (0) {}&lt;/pre&gt; is initializing _hwnd to 0 correct? Why exactly is this necessary? Is this just like when you set variables to zero before using them so they don't start with a random or invalid value? &lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/410197/410204/re-a-very-basic-question-about-classes/#410204</guid>
      <pubDate>Wed, 02 Dec 2009 13:15:33 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: A VERY basic question about classes</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/410197/410249/re-a-very-basic-question-about-classes/#410249</link>
      <description>&lt;span style="color: Blue;"&gt;You are correct.&lt;br /&gt;
It is the same as:&lt;/span&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
WinMaker::WinMaker ()
{
  _hwnd=0;
}
&lt;/pre&gt;&lt;br /&gt;
&lt;span style="color: Blue;"&gt;I am not sure how this is different from the code you posted, but I think it allows for a compiler to &lt;strong&gt;inline&lt;/strong&gt; the constructor code, making code execution faster. However, I may be wrong.&lt;/span&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/410197/410249/re-a-very-basic-question-about-classes/#410249</guid>
      <pubDate>Thu, 03 Dec 2009 05:43:08 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: A VERY basic question about classes</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/410197/410318/re-a-very-basic-question-about-classes/#410318</link>
      <description>Thanks for the reply. That explains one of my questions, but what about the class definitions:&lt;br /&gt;
WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst);&lt;br /&gt;
VS&lt;br /&gt;
WinClass (WNDPROC, char const *, HINSTANCE);&lt;br /&gt;
&lt;br /&gt;
Am I right in thinking the values wndProc, className, and hInst are not mandatory in the definition and just placeholders or clues to the programmer that the compiler ignores?&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/410197/410318/re-a-very-basic-question-about-classes/#410318</guid>
      <pubDate>Fri, 04 Dec 2009 07:49:33 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: A VERY basic question about classes</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/410197/410362/re-a-very-basic-question-about-classes/#410362</link>
      <description>&lt;span style="color: Blue;"&gt;You are correct again! When compiler "sees" a prototype:&lt;br /&gt;
&lt;br /&gt;
ret_type func_name (...parameters...) ;&lt;br /&gt;
&lt;br /&gt;
it only needs to detect parameter types, their names are not important, it is just more readable for a programmer if names are actually there, because proper naming can explain code better.&lt;br /&gt;
&lt;br /&gt;
C++ also can accept the omitted parameter names in a body of function - not only in prototype. Example:&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
void function (int a, int b)
{
  printf ("%d", a);
}
&lt;/pre&gt;&lt;br /&gt;
&lt;span style="color: Blue;"&gt;In the above code parameter &lt;strong&gt;b&lt;/strong&gt; is not used. Strict compiler will give a warning for that. To shut up the compiler - the name simply can be omitted:&lt;/span&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
void function (int a, int)
{
  printf ("%d", a);
}
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/410197/410362/re-a-very-basic-question-about-classes/#410362</guid>
      <pubDate>Sat, 05 Dec 2009 05:43:22 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: A VERY basic question about classes</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/410197/410384/re-a-very-basic-question-about-classes/#410384</link>
      <description>Cool. I think I remember reading somewhere in one of my books you could do that in functions. Making the comparison between them and class definitions helps me wrap my head around things a bit better, thanks.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/410197/410384/re-a-very-basic-question-about-classes/#410384</guid>
      <pubDate>Sat, 05 Dec 2009 15:33:38 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
  </channel>
</rss>