Thread: simple question on passing parameters

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    simple question on passing parameters

    Hello folk,
    I have to code one thing like here below but I'm confusing on use of ...
    Ho can I do that for with ...?
    Code:
    void Tree::createTree (Node* params ...) {
    	for (int i=0;   ??????? ; ++i) {
    		appendNode(params[i]);
    	}
    }
    //main
    mytree.createTreee( new Node (1,12), new Node (1,2), new Node(20,10) );
    Furthermore: Is there an alternative way?

    thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to create an array and pass the array and the size of the array to the function.

    A better choice would be to have a vector<Node> and pass that to the function. Then you can push_back Nodes in main until you are done and pass the full vector. The vector knows it's own size, so you would use that in the loop.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst I think it's probably not a good design to use ... [because it tends to cause "bad code" and "subtle errors"], the following code is something I just hacked up to show the mechanisms of variable arguments:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstdarg>
    
    using namespace std;
    
    class Node 
    {
    private:
    	int n;
    public:
    	Node(int x) : n(x) {};
    	void printMe() const { cout << n << endl; };
    };
    
    class A {
    public:
    	void DoSomething(int argCount, ...);
    };
    
    void A::DoSomething(int argCount, ...)
    {
    	va_list v;
    	Node *node;
    	va_start(v, argCount);
    	for(int i = 0; i < argCount; i++)
    	{
    		node = va_arg(v, Node *);
            node->printMe();
    		delete node;
    	}
    	va_end(v);
    }
    
    int main( )
    {
    	A a;
    	a.DoSomething(3, new Node(1), new Node(2), new Node(3));
    	return 0;
    }
    Note that you have to have at least one parameter that is names. If you don't want to use a count, you could have a "marker" at the end, such as a "NULL" or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  2. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  3. A question about passing arguments
    By AntiScience in forum C++ Programming
    Replies: 10
    Last Post: 10-16-2007, 02:42 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. ! C question: simple integer function passing
    By aaronc in forum C Programming
    Replies: 10
    Last Post: 04-29-2004, 01:18 AM