A Function prototype I don't recognize...

This is a discussion on A Function prototype I don't recognize... within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm working through a Win32 C++ tutorial here: http://www.relisoft.com/win32/index.htm and there's a function definition (and implementation) within a class ...

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Unhappy A Function prototype I don't recognize...

    Hi, I'm working through a Win32 C++ tutorial here: http://www.relisoft.com/win32/index.htm and there's a function definition (and implementation) within a class definition that I don't recognize. I wouldn't be too bothered but they use it all the time. Here's the code:

    Code:
    class WinSimpleClass
    {
    	public:
    		WinSimpleClass (char const * name, HINSTANCE hInst)
    			: _name (name), _hInstance (hInst)
    		{}
    		
    		WinSimpleClass (int resId, HINSTANCE hInst);
    		char const * GetName () const { return _name.c_str (); }
    		HINSTANCE GetInstance () const { return _hInstance; }
    		HWND GetRunningWindow ();
    	
    	protected:
    		HINSTANCE	_hInstance;
    		std::string	_name;
    };
    The bit I don't understand is this:

    Code:
    		WinSimpleClass (char const * name, HINSTANCE hInst)
    			: _name (name), _hInstance (hInst)
    		{}
    Can anyone explain to me what this colon, followed by what appears to be two typecasts of some description(???!!), that precedes the function body means? (i've looked through just about every C++ tutorial to no avail )

    Any help would be much appreciated.

    Thanks,

    Dom

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    WinSimpleClass() is the constructor for the WinSimpleClass class and the values after the : are member variables of class WinSimpleClass that are being initialised as:
    _name = name
    _hInstance = hInst

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    WinSimpleClass (char const * name, HINSTANCE hInst)
    			: _name (name), _hInstance (hInst)
    
    		{}
    That code defines a constructor for the WinSimpleClass and,

    : _name (name), _hInstance (hInst)

    is called an initializer list. The result of the above code is the same as doing this:

    Code:
    WinSimpleClass (char const * name, HINSTANCE hInst)
    }			
    	_name = name;
    	_hInstance = hInst;
    }
    However, the process is different. When you use an intializer list, the initial value is used to initialize the data member as it's created, which can be a much more efficient process than using assignment statements in the body of the constructor, particularly if the data member is a class instance.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Plus, the only way to initalize constants in a class is via the initalizer list.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Many thanks guys,

    everything is now clear :-))

    Dom
    Last edited by DominicTrix; 04-12-2003 at 05:53 PM.
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. call to function without prototype
    By lgbeeb in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 12:20 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21