简体   繁体   中英

Initialising an array in a class in C++

I am writing a class for a stack with 4 elements. 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.

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];
}

在HPStack的构造函数中,您已经初始化了本地数组而不是成员数组,您是否注意到了?

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM