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!
Printable View
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!
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.
int f(int, ... );
int f(int, ... ) {
.
.
.
}
int g() {
f(1,2,3);
}
Note that where it says '...' that's literally the symbol you use
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 ?
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.Quote:
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....?
I am not talking about function overloading. I am talking about virtual function overriding, e.g.,Quote:
Originally Posted by Dave1024
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;
};
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.Quote:
Originally Posted by Dave1024
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 ;)