Defining function arguments

This is a discussion on Defining function arguments within the C++ Programming forums, part of the General Programming Boards category; I am reading on this site and they have this code Code: HWND Create(HINSTANCE hinst, LPCTSTR clsname, LPCTSTR wndname, HWND ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Defining function arguments

    I am reading on this site and they have this code

    Code:
    HWND Create(HINSTANCE hinst, LPCTSTR clsname,
    		   LPCTSTR wndname, HWND parent = NULL,
    	            DWORD dStyle = WS_OVERLAPPEDWINDOW,
    		   int x = CW_USEDEFAULT, int y = CW_USEDEFAULT,
    		   int width = 450, int height = 380);
    HWND parent = NULL

    ? you can assign a variable in the functions declaration? How does this work?
    What is C++?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It is a default value. If the HWND parameter is not passed, then it defaults to NULL.

    Here is another example:

    void foo(int x = 10);

    You can call the function foo as:
    foo(15);
    or
    foo();

    In the second case, x is given the default value of 10.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    ..

    I have been programming for 2-3 years now... and I have wondered about this forever...

    I cant believe I didnt know this, thank you.

    [edit]
    I also noticed this

    BOOL CanShow = ::ShowWindow(hwnd, dCmdShow);

    What is the :: prefix?
    Last edited by Vicious; 09-13-2004 at 01:02 AM.
    What is C++?

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I am reading a great book:
    <Core C++: A Software Engineering Approach>

    it's discuss deeply about function() and class and dynamic memory .etc..
    it's very hard to comprehend for a newbie like me , but it's great!

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    whenever i see :: scope resolution operator in front of something with no left hand operand i think it's referrencing a globally defined object
    Last edited by The Brain; 09-13-2004 at 01:20 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    In that case, the :: is used to indicate that you are calling a Win32 API function. This is sometimes necessary because MFC functions and Win32 functions will have the same name.

    For instance, GetWindowText() will call the MFC function, but ::GetWindowText() will call the Win32 API function.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The :: operator can be used to explicitly access a globally defined object (in the 'unnamed' namespace)

    For example, this program will output 3:
    Code:
    int x = 0;
    int main()
    {
      int x = 3; //local x hides the global x
      ::x = 4; //access global x explicitly
      cout x;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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