How do you use "..."?

Started by
15 comments, last by Way Walker 17 years, 11 months ago
How do you go about creating and using a function with ... as a parameter? What I'm trying to do is achieve a method something a long the lines of scanf() (but different).
Advertisement
This is called variable numbers of arguments or varargs. This might help: http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html
You don't. Really. Please. Trust me on this. Using the ellipsis construct is a HORRIBLE idea in C++. It was kept around for C compatibility, but it breaks type safety, and means that object destruction may or may not be handled correctly. Learn from how iostreams works.
Quote:Original post by Sneftel
You don't. Really. Please. Trust me on this. Using the ellipsis construct is a HORRIBLE idea in C++. It was kept around for C compatibility, but it breaks type safety, and means that object destruction may or may not be handled correctly. Learn from how iostreams works.

What do you mean?

I'm going to be using this to create a method to pass arguements to a script function in C++ using AngelScript.
Seconded that the elipses is bad practice, but if you really want to know:

Clicky.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
Quote:Original post by PumpkinPieman
Quote:Original post by Sneftel
You don't. Really. Please. Trust me on this. Using the ellipsis construct is a HORRIBLE idea in C++. It was kept around for C compatibility, but it breaks type safety, and means that object destruction may or may not be handled correctly. Learn from how iostreams works.

What do you mean?


If you pass in a whole bunch of variables, you have no way of knowing what variable is of what type, and things tend to get messy. A variable of one type might be assumed to be another, which would be a Bad Thing. There are much better ways of acomplishing the same task.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
Alright, well. Is there some type of method besides this that will allow me to send parameters of an unknown amount?
Quote:Original post by PumpkinPieman
Alright, well. Is there some type of method besides this that will allow me to send parameters of an unknown amount?


Operator chaining works, if you don't mind altering the syntax of your calls. Take std::cout << 10 << ' ' << 30; for example. Otherwise you could pass your parameters in a std::vector<boost::any std::vector<boost::variant<int,double,std::string> > or something similar.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I don't use the boost library, is there any other way?
What do you mean 'you don't use it' ? Use it. It's better than writing a buggy less feature complete version of the exact same thing all by yourself.

This topic is closed to new replies.

Advertisement