CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2008
    Posts
    168

    Variable Arguments

    Plz help to Solve the following Questions

    Is it possible variable arguments in C++ functions ,

    What is the syntax and prerequired functions for this if yes....

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Variable Arguments

    There is support for variable argument lists inherited from C, except that the relevant standard header is <cstdarg> instead of <stdarg.h>. However, an idiom more common in C++ is to use function chaining instead. If you tell us why you are trying to use a variable argument list, perhaps we can tell you how to use function chaining instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Variable Arguments

    int f(int, ... );

    int f(int, ... ) {
    .
    .
    .
    }

    int g() {
    f(1,2,3);
    }

    Note that where it says '...' that's literally the symbol you use
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  4. #4
    Join Date
    Apr 2008
    Posts
    168

    Re: Variable Arguments

    I am thinking about the var:: arg function when i create a draw()...Different arg list for different shapes.so different fn() for different shapes....How to use fn chaining here ?

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Variable Arguments

    Quote Originally Posted by Dave1024
    I am thinking about the var:: arg function when i create a draw()...Different arg list for different shapes.so different fn() for different shapes.
    Why exactly would there be a different parameter list for each different shape? It sounds like an appropriate solution is to create an abstract base class with a pure virtual draw() member function, and then override it appropriately for each shape.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 2008
    Posts
    168

    Re: Variable Arguments

    Ok Might be possible. But i have this situation

    int Shape(int,int,int); Arguments For a Triangle

    int Shape(int,int,int,int,int); Arguments For a Pentagon

    int Shape(int,int,int,int,int,int) ;Arguments for a hexagon

    this is ok with function overloading in C++;

    Here var:: arg will avoid the overhead of overloading fns declaration and there is

    one and only one fn Shape(int,...);

    Is this an Advantage....?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Variable Arguments

    Quote Originally Posted by Dave1024
    But i have this situation

    int Shape(int,int,int); Arguments For a Triangle

    int Shape(int,int,int,int,int); Arguments For a Pentagon

    int Shape(int,int,int,int,int,int) ;Arguments for a hexagon

    this is ok with function overloading in C++;
    I am not talking about function overloading. I am talking about virtual function overriding, e.g.,
    Code:
    class Shape
    {
    public:
        virtual ~Shape() {}
        virtual void draw(std::ostream& out) const = 0;
    };
    
    class Triangle : public Shape
    {
    public:
        Triangle(int x, int y, int z) : x(x), y(y), z(z) {}
    
        virtual void draw(std::ostream& out) const
        {
            out << "Triangle: " << x << ", " << y << ", " << z << '\n';
        }
    private:
        int x, y, z;
    };
    
    class Pentagon : public Shape
    {
    public:
        Pentagon(int v, int w, int x, int y, int z) : v(v), w(w), x(x), y(y), z(z) {}
    
        virtual void draw(std::ostream& out) const
        {
            out << "Pentagon: " << v << ", " << w << ", " << x << ", " << y << ", "
                << z << '\n';
        }
    private:
        int v, w, x, y, z;
    };
    Quote Originally Posted by Dave1024
    Here var:: arg will avoid the overhead of overloading fns declaration and there is

    one and only one fn Shape(int,...);

    Is this an Advantage....?
    What overhead are you talking about? Virtual functions do come with some overhead, but that does not apply to function overloading, other than more code. Besides, you cannot just provide 3 arguments for a triangle with a variable argument list. You need another argument to tell the function how many arguments there are.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Variable Arguments

    Quote Originally Posted by Dave1024 View Post
    Ok Might be possible. But i have this situation

    int Shape(int,int,int); Arguments For a Triangle

    int Shape(int,int,int,int,int); Arguments For a Pentagon

    int Shape(int,int,int,int,int,int) ;Arguments for a hexagon

    this is ok with function overloading in C++;

    Here var:: arg will avoid the overhead of overloading fns declaration and there is

    one and only one fn Shape(int,...);

    Is this an Advantage....?
    That looks like a broken design. If you have got only one shape class that can represent a triangle, a square and maybe some other shapes you better create one abstract shape base class and introduce a derived class for every concrete shape type. This is the example used by almost every C++ book I read to introduce polymorphism
    - Guido

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured