Is there any benefit to having this:
OPTION 1:
Code:
class stackArray{
static const int STACK_SIZE = 3;
public:
stackArray(){
item = new int[STACK_SIZE];
item[0] = 100;
item[1] = 101;
item[2] = 102;
};
int *item;
};
over this:
OPTION 2:
Code:
class stackArray{
static const int STACK_SIZE = 3;
public:
stackArray(){
item[0] = 100;
item[1] = 101;
item[2] = 102;
};
int item[STACK_SIZE];
};
I would think that option 2 could cause dangling pointers but when I test it, it isn't the case. I tested option 2 with the following code:
Code:
stackArray createObject(){
stackArray instance1;
return instance1;
}
int main (){
cout << createObject().item[0] << endl;
cout << createObject().item[1] << endl;
cout << createObject().item[2] << endl;
return 0;
}
And it printed out the array the same as option 1 with no garbage values.