开发者

Initialising an array in a class in C++

I am writing a class for a stack with 4 el开发者_StackOverflow中文版ements. Defined like this:

// HPStack.h
class HPStack{
public:
        HPStack();
        void push(int x);
        int  pop();
        int  peek();
private:
        int stack[];
};
 // HPStack.cpp
HPStack::HPStack(){
        int stack[4] = {0,0,0,0};
}
// push/pop functions
// ....
int HPStack::peek(){
        return stack[0];
}

Then I call it with:

int main(){
        HPStack* stack = new HPStack();
        cout << stack->peek() << endl;
        return 0;
}

But when I run the main function (compiled with g++), it outputs: 137048 when I actually want it to print 0. What is going on here and what can I do to fix the problem?


HPStack::HPStack(){
        int stack[4] = {0,0,0,0};
}

Here, stack is declared as a local variable. If your class has a member variable also called stack then it is not initialized and not visible in the constructor because it has been hidden.

If you want to zero-initialized a member array, you can value-initialize it by giving it an explicit empty initializer in the member initializer list.

HPStack::HPStack() : stack()
{
}

Edit: This is an illegal member definition. If you have an array member you must give it a non-zero size:

private:
    int stack[];


 int stack[4] = {0,0,0,0};

is a local variable and goes out of scope once the constructor returns. stack[] as a member variable is different from the one present in constructor.


In the constructor of HPStack you've initialized local array instead of member-array, have you noticed?


I assume you have int stack[4] as the member variable of HPStack. But in constructor you are doing int stack[4] = {0,0,0,0};, the extra int here results in a new local variable stack which hides the original member variable. So when you use peek the member variable is uninitialized and you get a garbage value. You need to correct your initialization code, take a look at the std::fill method which can fill the already defined array with a given value.


int stack[4] = {0,0,0,0};

This creates a local stack variable - it's not modifying your member stack variable. Change it to:

std::fill(stack, stack+4, 0);

Or simply:

for (int i = 0; i < 4; ++i) 
    stack[i] = 0;

Also, your stack member variable should be declared like this:

int stack[4];

Updated code:

// HPStack.h
class HPStack{
public:
        HPStack();
        void push(int x);
        int  pop();
        int  peek();
private:
        int stack[4];
};
 // HPStack.cpp
HPStack::HPStack(){
        for (int i = 0; i < 4; ++i) 
            stack[i] = 0;
}
// push/pop functions
// ....
int HPStack::peek(){
        return stack[0];
}


In your constructor you must dynamically allocate memory for your array.

stack = new int[4];

Or you can make a static array in your class definition

int stack[4];

PS: when you use dynamic allocation don't forget to free memory in your destructor:

delete[] stack;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜