A function accepting a variable number of arguments.

This is a discussion on A function accepting a variable number of arguments. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a wrapper for the Windows API that will let me use a single function to replace ...

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

    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:

    Code:
    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.

    http://www.experts-exchange.com/Prog..._20553366.html
    http://www.codeproject.com/cpp/argfunctions.asp

    Help would be appreciated.

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

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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
    Posts
    712
    I don't know if this is going to help you:
    Variable String Arguments

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, 06:18 AM
  2. Variable number of arguments in function.
    By kamoda_pawel in forum C Programming
    Replies: 1
    Last Post: 01-18-2007, 06: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, 07:55 AM
  5. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 04:14 AM

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