Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: C++ question

  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question C++ question

    Hi there,

    I have a question:

    A function with variable number of arguments:

    1) does it have only one prototype declaration?

    or

    2) Should it have as many as prototype declaration to be used to actual functions used with different parameters?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You would need to have one declaration for each number of arguments. C++ does not support variable number of arguments directly.

    However there are ways around it, if the arguments are the same type, you could put them in a vector and pass that. Then you would have one function prototype and definition and the vector can have as many elements as you want.
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    So which is the most appropriate answer out of those 2 options?

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Soon variadic templates will be the correct answer.

    Right now it's impossible to say what is the most sensible route without knowing more about what you're trying to accomplish.

    If your function accepts an arbitrary number of homogeneous arguments, accepting a vector or similar container of them is probably the best.

    If you need heterogeneous arguments you might be best off using a functor:

    Code:
    class VarArgs
    {
    public:
    	VarArgs& operator()(const std::string& arg)
    	{
    		strings.push_back(arg);
    		return *this;
    	}
    
    	VarArgs& operator()(const int arg)
    	{
    		ints.push_back(arg);
    		return *this;
    	}
    
    	void operator()() const
    	{
    		std::cout << "Integers: ";
    		std::copy(ints.begin(), ints.end(), std::ostream_iterator<int>(std::cout, " "));
    		std::cout << "\nStrings: ";
    		std::copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(std::cout, " "));
    		std::cout << std::endl;
    	}
    private:
    	std::vector<int> ints;
    	std::vector<std::string> strings;
    };
    
    int main()
    {
    	VarArgs va;
    	va("test")(4)(2)("asdf")("foobar")(9)();
    }
    If a class's operator() returns a reference to the instance it can be chained together. You can store the argument context however it makes sense and then do the actual work in operator()(void).
    Last edited by ralph l mayo; 08-07-2009 at 04:23 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •