I got problem with va_list. I used it in constructor to get unexpected amount of data to my object. The problematic code is there:
#include <cstdarg>
#include <iostream>
using namespace std;
class DATA{
private:
int length;
int* tab;
public:
DATA():tab(NULL), length(0){};
DATA(int x, ...):length(x+1){
va_list ap;
va_start(ap, x);
tab=new int[length];
for(int i=0; i<length; ++i){
tab[i]=va_arg(ap, int);
}
va_end(ap);
}
void showthem()const{
if(tab!=NULL){
int x;
for(x=0; x<length-1; ++x){
cout<<tab[x]<<", ";
}
cout<<tab[x];
}
}
}
ostream & operator<<(ostream & out, const DATA & x){
x.showthem();
return out;
}
int main(){
int table [] = {5,1,2,3,4,5,6};
DATA x1(*table);
DATA x2(4,1,2,3,4,5);
cout << x1 << endl;
cout << x2 << endl;
}
When I make object naturally by writing all parameters it's okay, but when i try to make it by a table it making problem. I got unexpected data in class tab.
I guess I am making something wrong. In forst way - can I even make object like this via *table? I am giving to the constructor some amount of integers so it should work...