|
-
June 8th, 2009, 01:06 AM
#1
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!
-
June 8th, 2009, 01:12 AM
#2
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.
-
June 8th, 2009, 01:20 AM
#3
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
-
June 8th, 2009, 01:45 AM
#4
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 ?
-
June 8th, 2009, 01:52 AM
#5
Re: Variable Arguments
 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.
-
June 8th, 2009, 04:19 AM
#6
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....?
-
June 8th, 2009, 04:37 AM
#7
Re: Variable Arguments
 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;
};
 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.
-
June 8th, 2009, 04:56 AM
#8
Re: Variable Arguments
 Originally Posted by Dave1024
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|