Jump to content

Variable number of arguments

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Before you ask, va_list doesn't cut it because arguments are objects (va_list only works for plain data types) and I also don't intend to wait for C++0x.

So, I'm looking for a way to pass a variable number of arguments to a class method. I'd like to pass arguments not in array/vector or anything like that. If anyone has idea(s), do tell!
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Is there a reason you can't use vectors?

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You could:
1) Using BlainSch's idea, make a superclass of everything that doesn't do anything and use that for the vector
2) Use pointers to all classes passed instead of passing the classes themselves. Since pointers are primitives, you should be fine.
sudo rm -rf /

#4
Smilex

Smilex

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
you can't do function(int count, ...) ? Haven't tried it myself for some time, but I think I remember doing this before C++0x

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

Smilex said:

you can't do function(int count, ...) ? Haven't tried it myself for some time, but I think I remember doing this before C++0x
Yes you can, which is why I was saying to pass pointers so you could get primitive datatypes. You'll need va_args to access the rest of the arguments.
sudo rm -rf /

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@BlaineSch, no reason really, I just think it looks prettier that way. :)

Solved with pointers, thanks. I have another sub question: I'm parsing a text file and some lines may have 3 or 4 (or more, but highly unlikely) integer values. Best method to parse them I can think of is to read whole line, split that line by spaces (values are separated by spaces) and then count elements. Any better ideas?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Do you need to keep track of what integers are on which lines? If not, you can use fscanf() for C, and an std::ifstream object (include <fstream>) for C++. Both use spaces as delimiters by default. If you do need to keep track of lines, you can read in a line and use strtok() or an std::istringstream object (include <sstream>) to do the job.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users