Functions with variable arguments

Aug 27, 2010 at 1:35am
va_start takes two arguments, the va_list and the name of the last specified argument.

Is it possible to not have a last specified argument? If so, how would you call va_start?

For example, could I have:

1
2
3
void exampleFunc(...) {
    // Code
}


If that is possible, how would you call va_start? Would va_start(va_list, void) work?
Aug 27, 2010 at 1:40am
I think that it uses the argument to get the memory address of it and thus find the addresses for the other variables. Although I want to know how to do this too, wouldn't you still want a parameter to represent the number of arguments or such?
Last edited on Aug 27, 2010 at 1:40am
Aug 27, 2010 at 1:43am
Logically, but I was hoping there would also be a way to count the number of arguments without having to specify the number in an argument. I can't find anything on that either.
Aug 27, 2010 at 1:44am
You can't. Without that first parameter, the function can't know where the other parameters are, just like how you can't know where element i of an array is without knowing where the array begins.

What are you trying to do? It can probably be done some other (better) way.

EDIT:
I was hoping there would also be a way to count the number of arguments without having to specify the number in an argument
That's also not possible.
Last edited on Aug 27, 2010 at 1:46am
Aug 27, 2010 at 1:46am
How would that variable be stored? Well, in the memory with the other arguments, of course! So I don't see that much of a point in not including an argument to represent the number of additional ones.
Aug 27, 2010 at 1:53am
Well, I'm creating an object in C++ that will draw shapes in OpenGL. I want the Shape (a class I made) object to be able to draw Shapes based on the number of Points (also a class I made) passed to it.

Passing the number of points in it's own argument wouldn't be a big deal there, but where the issue comes in is that I have a function in Shape called setColors, which is designed to set the color of each vertex. I wanted the function to be able to just loop through the array of Points created, without having to specify the number of points through via argument. I mean, I can, but it seemed like one parameter more than I needed.
Aug 27, 2010 at 2:05am
How's this?
1
2
3
4
Plane p;
p <<Point(0,0,0)<<Color(0,0,0)
  <<Point(0,1,0)<<Color(0,1,0)
  <<Point(0,1,1)<<Color(1,0,0);
Aug 27, 2010 at 2:13am
Uh... I'm not really sure what that does.

The only time I've ever seen << is with cout.

I must warn you that I'm only mediocre with C++ and actually just learning OpenGL. So some advanced things, I don't really understand. And what you've done may work, but I don't really like to use things I don't understand, so would you mind explaining what is going on with that? I don't want to be a pest =/
Aug 27, 2010 at 2:22am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct Color{
    double channel[3];
    Color(){}
    Color(double r,double g,double b);
};

struct Point{
    double coord[3];
    Point(){}
    Point(double x,double y,double z);
};

class Plane{
    std::vector<Point> vertices;
    std::vector<Color> colors;
public:
    Plane(){}
    Plane &operator<<(const Point &p){
        this->vertices.push_back(p);
        return *this;
    }
    Plane &operator<<(const Color &c){
        this->colors.push_back(c);
        return *this;
    }
};
Last edited on Aug 27, 2010 at 2:22am
Aug 27, 2010 at 2:27am
Ah, I see now.

I've never messed with custom operators either xD, but I understand it. Vectors are also something new for me, but I went and looked them up. My only other question would be why struct versus class?
Aug 27, 2010 at 2:33am
No reason. I just didn't need any of Color's and Point's members to be private.
Aug 27, 2010 at 2:36am
Oh. Good point =)

That would seemingly work much better than my idea. I also like the word Plane better than Shape xD

Thank you for your suggestion and for teaching me new things. They will be useful not only for this project but for many to come.
Aug 27, 2010 at 2:45am
A struct is for C what a class is for C++ except that the contents of a struct are public by default where as the contents of a class are private by default.
Mr Stroustrup recommends that struct be used in C++ when the contents of the body are all public. This minor economy is presumably why helios uses it. But it may say more between experts
Topic archived. No new replies allowed.