I'm in a class on C++. We've learned about overloading functions, and I decided to mix it with varying numbers of arguments (not a topic we covered in class; something I just know from studying C on my own.)
Anyways, I have a stack class with an overloaded constructor. Here is most of the class:
My issue is that I'd like to call Stack::Stack (int count, ...), but every time I try that, it's calling Stack::Stack (int maximum, int count, ...) instead. Here's the code:
Here's the output:
The output I'd rather have would be:
Edit: This also brings up an interesting order of operations issue... I just realized the numbers were printing in an order opposite of what I was expecting... a little research seems to reveal that << evaluates from right to left, but then prints from left to right?
Anyways, I have a stack class with an overloaded constructor. Here is most of the class:
Code:
class Stack {
private:
int *data;
unsigned int max;
unsigned int size;
int top;
public:
Stack(unsigned int, unsigned int, ...);
Stack(unsigned int, ...);
Stack();
~Stack();
void push(int);
int pop();
};
Stack::Stack (unsigned int maximum, unsigned int count, ...)
{
cout << "Stack::Stack (int maximum, int count, ...)" << endl;
if (max < count) max = maximum = count;
data = new int[max];
size = count;
va_list values;
va_start(values, count);
for (int i = 0; i < count; i++)
{
data[i] = va_arg(values, int);
}
va_end(values);
top = data[count-1];
}
Stack::Stack (unsigned int count, ...)
{
cout << "Stack::Stack (int count, ...)" << endl;
max = (MAX_SIZE < count)?count:MAX_SIZE;
data = new int[max];
size = count;
va_list values;
va_start(values, count);
for (int i = 0; i < count; i++)
{
data[i] = va_arg(values, int);
}
va_end(values);
top = data[count-1];
}
Stack::Stack ()
{
cout << "Stack::Stack ()" << endl;
data = new int[MAX_SIZE];
max = MAX_SIZE;
size = top = 0;
}
My issue is that I'd like to call Stack::Stack (int count, ...), but every time I try that, it's calling Stack::Stack (int maximum, int count, ...) instead. Here's the code:
Code:
int main (int argc, char* argv[])
{
Stack a (5, 4, 3, 2, 1, 0);
cout << a.pop() << a.pop() << a.pop() << a.pop() << a.pop() << endl;
return 0;
}
Here's the output:
Code:
Stack::Stack (int maximum, int count, ...)
32100
The output I'd rather have would be:
Code:
Stack::Stack (int count, ...)
43210
Edit: This also brings up an interesting order of operations issue... I just realized the numbers were printing in an order opposite of what I was expecting... a little research seems to reveal that << evaluates from right to left, but then prints from left to right?
Last edited: