Click here to Skip to main content
15,861,125 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is the difference in C and C++ in terms of memory management ? Pin
jschell21-Mar-23 5:41
jschell21-Mar-23 5:41 
QuestionC language Pin
Pavani M18-Mar-23 5:24
Pavani M18-Mar-23 5:24 
AnswerRe: C language Pin
Richard MacCutchan18-Mar-23 6:53
mveRichard MacCutchan18-Mar-23 6:53 
AnswerRe: C language Pin
CPallini19-Mar-23 22:33
mveCPallini19-Mar-23 22:33 
QuestionMessage Closed Pin
5-Mar-23 5:33
Member 149687715-Mar-23 5:33 
AnswerRe: Reorganizing code Pin
Richard MacCutchan5-Mar-23 21:56
mveRichard MacCutchan5-Mar-23 21:56 
QuestionMessage Closed Pin
3-Mar-23 5:42
Member 149687713-Mar-23 5:42 
AnswerRe: How to implement variable amount of parameters? Pin
Mircea Neacsu3-Mar-23 6:10
Mircea Neacsu3-Mar-23 6:10 
Easy peasy Smile | :)
C++
#include <stdargs>
void ProcessCommand (char* command, ...)
{
  va_list params; //opaque structure needed to process variable arguments lists

  va_start (params, command); //initialize with the last fixed argument before ellipsis

  // You need to know how many arguments to expect. Assume that args_count function tells you that
  // args_count is your function not some standard function
  int narg=args_count(command);
  for (i=0; i<narg; i++)
  {
    //Get next argument
    QString* pstr = va_arg (params, (QString*));

    // do something with the string
  }
  va_end(params); //cleanup

One of the problems with this code is that you need to know how many arguments to expect and the type of each one. Functions like printf explore format string and figure out from the '%' specifiers. There are countless stories about bugs occurring because they don't match. Sometimes, specially if your arguments are of the same type, you can add a count parameter just before the variable part:
C++
void ProcessCommand (char* command, int count, ...)


Another thought: if you know that all parameters are of the same type, why not pass an array. Something like this:
C++
void ProcessCommand (char *command, std::vector<QString*> verify)
{
  for (auto& pstr : verify)
  {
    //do something with it
  }
}

Mircea

AnswerRe: How to implement variable amount of parameters? Pin
k50543-Mar-23 6:18
mvek50543-Mar-23 6:18 
AnswerRe: How to implement variable amount of parameters? Pin
Richard MacCutchan3-Mar-23 21:57
mveRichard MacCutchan3-Mar-23 21:57 
AnswerRe: How to implement variable amount of parameters? Pin
jschell6-Mar-23 6:20
jschell6-Mar-23 6:20 
GeneralRe: How to implement variable amount of parameters? Pin
charlieg19-Mar-23 11:47
charlieg19-Mar-23 11:47 
QuestionC++ non-null pointers Pin
Mircea Neacsu2-Mar-23 7:47
Mircea Neacsu2-Mar-23 7:47 
AnswerRe: C++ non-null pointers Pin
jschell6-Mar-23 6:22
jschell6-Mar-23 6:22 
GeneralRe: C++ non-null pointers Pin
Mircea Neacsu6-Mar-23 7:34
Mircea Neacsu6-Mar-23 7:34 
QuestionMessage Closed Pin
1-Mar-23 7:32
Member 149687711-Mar-23 7:32 
AnswerRe: Compiler academic question - why? Pin
Mircea Neacsu1-Mar-23 7:37
Mircea Neacsu1-Mar-23 7:37 
GeneralRe: Compiler academic question - why? Pin
CPallini2-Mar-23 20:49
mveCPallini2-Mar-23 20:49 
GeneralRe: Compiler academic question - why? Pin
Mircea Neacsu3-Mar-23 0:37
Mircea Neacsu3-Mar-23 0:37 
AnswerRe: Compiler academic question - why? Pin
k50541-Mar-23 7:50
mvek50541-Mar-23 7:50 
QuestionMessage Closed Pin
21-Feb-23 8:09
Member 1496877121-Feb-23 8:09 
AnswerRe: painted myself into a corner - again - need HELP Pin
Richard MacCutchan21-Feb-23 8:26
mveRichard MacCutchan21-Feb-23 8:26 
AnswerRe: painted myself into a corner - again - need HELP Pin
Mircea Neacsu21-Feb-23 8:49
Mircea Neacsu21-Feb-23 8:49 
AnswerRe: painted myself into a corner - again - need HELP Pin
David O'Neil21-Feb-23 15:32
professionalDavid O'Neil21-Feb-23 15:32 
GeneralMessage Closed Pin
19-Feb-23 4:20
Member 1496877119-Feb-23 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.