variables arguments

Discussion in 'C++' started by mosfet, Feb 15, 2007.

  1. mosfet

    mosfet Guest

    Hi,

    I have a class defined like this :

    class CScreenLib
    {
    public:

    CScreenLib(void);
    ~CScreenLib(void);


    static void OptimizeWidth(HWND hwndDlg, int cAffectedCtls, UINT
    nIDAffectedCtl, ...);
    };

    And I would like to wrapp it like this

    class CResizeDlg : public CDialog
    {

    ....

    protected:
    void OptimizeWidth(int cAffectedCtls, UINT nIDAffectedCtl, ...);
    };

    Actyally in my CDialog object I always have the same HWND in
    this->m_hWnd ) but when I declare

    void CResizeDlg::OptimizeWidth(int cAffectedCtls, UINT nIDAffectedCtl, ...)
    {
    // I am supposed to call CScreenLib::OptimizeWidth(this->m_hWnd, ... HOW).
    }


    how can i pass varargs to a varargs ?
    mosfet, Feb 15, 2007
    #1
    1. Advertising

  2. "mosfet" <> wrote in message
    news:45d459cf$0$19648$...
    > Hi,
    >
    > I have a class defined like this :
    >
    > class CScreenLib
    > {
    > public:
    > CScreenLib(void);
    > ~CScreenLib(void);
    >
    >
    > static void OptimizeWidth(HWND hwndDlg, int cAffectedCtls, UINT
    > nIDAffectedCtl, ...);
    > };
    >
    > And I would like to wrapp it like this
    >
    > class CResizeDlg : public CDialog
    > {
    >
    > ...
    >
    > protected:
    > void OptimizeWidth(int cAffectedCtls, UINT nIDAffectedCtl, ...);
    > };
    >
    > Actyally in my CDialog object I always have the same HWND in
    > this->m_hWnd ) but when I declare
    >
    > void CResizeDlg::OptimizeWidth(int cAffectedCtls, UINT nIDAffectedCtl,
    > ...)
    > {
    > // I am supposed to call CScreenLib::OptimizeWidth(this->m_hWnd, ... HOW).
    > }
    >
    >
    > how can i pass varargs to a varargs ?
    >


    You can't, unless you create a base version that takes a va_list type which
    you use to pass to arguments (much like the vprintf() style functions)

    - Sylvester
    Sylvester Hesp, Feb 15, 2007
    #2
    1. Advertising

  3. mosfet wrote:
    > Hi,
    >
    > I have a class defined like this :
    >
    > class CScreenLib
    > {
    > [..]
    > static void OptimizeWidth(HWND hwndDlg, int cAffectedCtls, UINT
    > nIDAffectedCtl, ...);
    > };
    >
    > And I would like to wrapp it like this
    >
    > class CResizeDlg : public CDialog
    > {
    >
    > ...
    >
    > protected:
    > void OptimizeWidth(int cAffectedCtls, UINT nIDAffectedCtl, ...);
    > };
    >
    > [..] but when I declare
    >
    > void CResizeDlg::OptimizeWidth(int cAffectedCtls, UINT
    > nIDAffectedCtl, ...) {
    > // I am supposed to call CScreenLib::OptimizeWidth(this->m_hWnd, ...
    > HOW). }
    >
    >
    > how can i pass varargs to a varargs ?


    You cannot. You need to add another function to your CScreenLib to
    accept va_list and call it from both 'OptimizeWith' and the derived
    class.

    See how 'fprintf' and 'vfprintf' are implemented in your compiler
    documentation/sources.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 15, 2007
    #3
  4. mosfet

    Grizlyk Guest

    mosfet wrote:
    >
    > how can i pass varargs to a varargs ?


    This is example from help file:

    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
    Grizlyk, Feb 15, 2007
    #4
  5. Grizlyk wrote:
    > mosfet wrote:
    >>
    >> how can i pass varargs to a varargs ?

    >
    > This is example from help file:
    >
    >


    I wonder what is the relevance of your quote to the question.
    The example does not have anything about passing varargs to
    another varargs function.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 15, 2007
    #5
  6. mosfet

    Grizlyk Guest

    Victor Bazarov wrote:
    >>>
    >>> how can i pass varargs to a varargs ?

    >>
    >> This is example from help file:
    >>
    >>

    >
    > I wonder what is the relevance of your quote to the question.
    > The example does not have anything about passing varargs to
    > another varargs function.


    The unlear for your example is example from help file and shows how so rare
    used declarations, as
    void va_start(va_list ap, LAST_REQUIRED_ARG);
    TYPE va_arg(va_list ap, TYPE);
    void va_end(va_list ap);
    can be used.

    I think, if OP will see how "..." can be processed with the help of the
    declarations above, OP has enough wise to make his own example.


    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
    Grizlyk, Feb 15, 2007
    #6
  7. Grizlyk wrote:
    > Victor Bazarov wrote:
    >>>>
    >>>> how can i pass varargs to a varargs ?
    >>>
    >>> This is example from help file:
    >>>
    >>>

    >>
    >> I wonder what is the relevance of your quote to the question.
    >> The example does not have anything about passing varargs to
    >> another varargs function.

    >
    > The unlear for your example is example from help file and shows how
    > so rare used declarations, as
    > void va_start(va_list ap, LAST_REQUIRED_ARG);
    > TYPE va_arg(va_list ap, TYPE);
    > void va_end(va_list ap);
    > can be used.
    >
    > I think, if OP will see how "..." can be processed with the help of
    > the declarations above, OP has enough wise to make his own example.


    Are you just pretending to be inattentive? The OP's question was
    about the syntax to call

    void bar(int, ...);

    from

    void foo(int, ...) {
    ///
    bar(???); // here. Any suggestions?
    }

    And NOT about how to extract the varargs inside 'foo'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 15, 2007
    #7
  8. mosfet

    Grizlyk Guest

    Victor Bazarov wrote:
    >
    > Are you just pretending to be inattentive? The OP's question was
    > about the syntax to call
    >
    > void bar(int, ...);
    >
    > from
    >
    > void foo(int, ...) {
    > ///
    > bar(???); // here. Any suggestions?
    > }
    >
    > And NOT about how to extract the varargs inside 'foo'.


    Probably i did not gazing read. I think the trouble can be only to access to
    "..." params inside C++ function. Do not speak, that OP can not write as
    exmple below.

    Really, I do not why no standard way to lunch boo, so OP can make own
    function to lunch.

    -- cut here --

    #include <stdio.h>
    #include <stdarg.h>

    typedef unsigned uint;

    // ************************************
    // ************************************
    extern "C" void boo(uint size, ...)
    {
    va_list argptr;
    va_start(argptr, size);

    printf("boo: ");
    for(uint i=size; i; --i){ printf("%u ",va_arg(argptr,uint) ); }
    if(size)printf("\n");

    va_end(argptr);
    }


    // ************************************
    // ************************************
    #define FIXED_ARRAY_SIZE
    #define ARRAY_SIZE (1024)

    extern "C" void foo(uint size, ...)
    {
    if(!size){ boo(size); return; }

    //the most simple way
    // ************************************
    #ifdef FIXED_NUM_OF_PARAMS

    uint *buf=0;
    try{
    buf=new uint[size];
    uint *ptr=buf;

    va_list argptr;
    va_start(argptr, size);

    for(uint i=size; i; --i){ *ptr++=va_arg(argptr,uint); }

    va_end(argptr);

    switch(size)
    {
    case 1: boo(size,buf[0]); break;
    case 2: boo(size,buf[0],buf[1]); break;
    case 3: boo(size,buf[0],buf[1],buf[2]); break;
    case 4: boo(size,buf[0],buf[1],buf[2],buf[3]); break;
    case 5: boo(size,buf[0],buf[1],buf[2],buf[3],buf[4]); break;
    default:
    throw "overflow";
    }

    }catch(...){delete[] buf; throw;}
    #endif

    // ************************************
    #ifdef FIXED_ARRAY_SIZE

    struct Buf
    {
    uint array[ARRAY_SIZE];
    };

    Buf buf;
    uint *ptr=buf.array;

    va_list argptr;
    va_start(argptr, size);

    for(uint i=size; i; --i){ *ptr++=va_arg(argptr,uint); }

    va_end(argptr);

    boo(size,buf);
    /*
    //alloc stack area
    b4: 81 ec 0c 10 00 00 sub $0x1000,%esp

    //fill stack area
    bd: 89 e0 mov %esp,%eax
    bf: 52 push %edx
    c0: 52 push %edx
    c1: 68 00 10 00 00 push $0x1000
    c6: 56 push %esi
    c7: 50 push %eax
    c8: e8 33 ff ff ff call _memcpy
    cd: 83 c4 14 add $0x14,%esp

    //push size
    d0: 53 push %ebx
    d1: e8 3a ff ff ff call 10 <_boo>

    //free stack area
    d6: 81 c4 10 10 00 00 add $0x1000,%esp

    // ***********************
    In example "boo(size,buf);" constant $0x1000 can be runtime value

    if not, that "boo(size,buf);" can be implemented as external
    "C-linkage" user function implemented with external ASM

    extern "C" void lunch(uint size, uint *buf, void *boo);
    really, I do not why no standard way to lunch boo

    */
    #endif

    // ************************************
    #ifdef EXIST_ASM

    asm{

    //alloc stack area
    subl size,%esp

    //fill stack area
    movsl (buf->%esp)[size]
    pushl size
    call _boo
    popl %eax

    //free stack area
    addl size,%esp

    }

    #endif

    }

    // ************************************
    // ************************************
    int main()
    {
    foo(0);
    foo(1,1);
    foo(2,1,2);
    foo(3,1,2,3);
    foo(4,1,2,3,4);
    foo(5,1,2,3,4,5);
    }

    -- cut here --

    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
    Grizlyk, Feb 15, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    4,889
    Josiah Carlson
    Apr 6, 2004
  2. Neo
    Replies:
    10
    Views:
    644
    sushant
    Jan 20, 2005
  3. tutmann
    Replies:
    4
    Views:
    430
  4. jmborr
    Replies:
    1
    Views:
    401
    Stargaming
    Nov 3, 2007
  5. Replies:
    9
    Views:
    931
Loading...

Share This Page