Thread: A function accepting a variable number of arguments.

  1. #1
    Registered User
    Join Date
    Aug 2004
    San Diego, CA

    A function accepting a variable number of arguments.

    I'm trying to write a wrapper for the Windows API that will let me use a single function to replace code such as this:

    hMenu = CreateMenu();
    			hSubMenu = CreatePopupMenu();
    			AppendMenu(hSubMenu, MF_GRAYED, ID_FILE_NEW, "&New");
    			AppendMenu(hSubMenu, MF_GRAYED, ID_FILE_OPEN, "&Open...");
    			AppendMenu(hSubMenu, MF_GRAYED, ID_FILE_SAVE, "&Save");
    			AppendMenu(hSubMenu, MF_GRAYED, ID_FILE_SAVEAS, "&Save As...");
    			AppendMenu(hSubMenu, MF_SEPARATOR, NULL, NULL);
    			AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
    			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    			SetMenu(hWnd, hMenu);
    However, the problem comes when I try to make the single function. What I would like to do is something along the lines of this:

    wCreateMenu(char* poptitle, char* submenu1, char* submenu2, ... , char* submenuN) where you can add in as many sub-menus as you would like. The problem is, I don't know how to tell C++ that "I do not know how many variables will be passed, please be able to work with them."

    I found a couple of tutorials, but the way they explained it made absolutely no sense whatsoever. Here's the links to those.

    Help would be appreciated.

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Look into the va_arg macro.

  3. #3
    Registered User
    Join Date
    Oct 2004
    That codeproject article explained it quite well. Play around with it and use the debugger and couts to see whats going on.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    I don't know if this is going to help you:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Variable number of arguments in function.
    By kamoda_pawel in forum C Programming
    Replies: 1
    Last Post: 01-18-2007, 07:18 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM