Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variables arguments

Reply
Thread Tools

variables arguments

 
 
mosfet
Guest
Posts: n/a
 
      02-15-2007
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 ?


 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      02-15-2007

"mosfet" <(E-Mail Removed)> wrote in message
news:45d459cf$0$19648$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-15-2007
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


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-15-2007
mosfet wrote:
>
> how can i pass varargs to a varargs ?


This is example from help file:

Quote:

#include <stdarg.h>

void va_start(va_list ap, LAST_REQUIRED_ARG);
TYPE va_arg(va_list ap, TYPE);
void va_end(va_list ap);

Description
-----------

Used to write functions taking a variable number of arguments. Note
that these are actually macros, and not functions. You must prototype
the function with `...' in its argument list. Then, you do the
following:

* Create a variable of type `va_list'.

* Initialize it by calling `va_start' with it and the name of the
last required (i.e. non-variable) argument.

* Retrieve the arguments by calling `va_arg' with the `va_list'
variable and the type of the argument. As another alternative,
you can pass the `va_list' to another function, which may then use
`va_arg' to get at the arguments. `vprintf' is an example of this.

* Call `va_end' to destroy the `va_list'.

Be aware that your function must have some way to determine the number
and types of the arguments. Usually this comes from one of the required
arguments. Some popular ways are to pass a count, or to pass some
special value (like `NULL') at the end.

Also, the variable arguments will be promoted according to standard C
promotion rules. Arguments of type `char' and `short' will be promoted
to `int', and you should retrieve them as such. Those of type `float'
will be promoted to `double'.

Return Value
------------

`va_arg' returns the argument it fetched, the other macros return
nothing.

Portability
-----------

ANSI/ISO C C89; C99
POSIX 1003.2-1992; 1003.1-2001

Example
-------

int find_the_sum(int count, ...)
{
va_list ap;
int i;
int total = 0;

va_start(ap, count);
for (i = 0; i < count; i++)
total += va_arg(ap, int);
va_end(ap);
return total;
}

int other_function(void)
{
return find_the_sum(6, 1, 2, 3, 4, 5, 6);
}
--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-15-2007
Grizlyk wrote:
> mosfet wrote:
>>
>> how can i pass varargs to a varargs ?

>
> This is example from help file:
>
>
Quote:
[..]
>


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


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-15-2007

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

>>
>> This is example from help file:
>>
>>
Quote:
> [..]
>>

>
> 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/


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-15-2007
Grizlyk wrote:
> Victor Bazarov wrote:
>>>>
>>>> how can i pass varargs to a varargs ?
>>>
>>> This is example from help file:
>>>
>>>
Quote:
>> [..]
>>>

>>
>> 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


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-15-2007

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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments