首页 > 解决方案 > Why is the static array member variable showing nothing after calling the instance of the object?

问题描述

Currently working on Object Oriented Programming in c++ and having problems with an instance showing nothing changed from a method I've created.

The whole code is based off of this object I've created from a header file.

#ifndef DEQUE_H_
#define DEQUE_H_


#include <iostream>

const int CAPACITY = 5;
const int DEFAULT = -1;

class Deque
{
public:
    Deque();
    int get_size() const;
    bool is_empty() const;
    bool is_full() const;
    int operator[](int i) const;
    static Deque insert_tail(int);
private:
    int size_;
    static int array_[CAPACITY];
};

std::ostream & operator<<(std::ostream &, const Deque &);


#endif 

One of the problems I'm having is the insert_tail method that doesn't show any changes to my static array. In the cpp file itself.. these are the function declarations.

#

include <iostream>
#include "Deque.h"

Deque::Deque()
    :size_(0)
{

}
int Deque::array_[5] = {};
int Deque::get_size() const
{
    return size_;
}

bool Deque::is_full() const
{
    if (size_ == 5) return 1;
    else return 0;
}

bool Deque::is_empty() const
{
    if (size_!= 5) return 1;
    else return 0;
}

int Deque::operator[](int i) const
{

    int something = array_[i];
    return something;
}

Deque Deque::insert_tail(int x)
{
    Deque d;
    d.size_ += 1;
    int size = d.size_;
    d.array_[size - 1] = x;

    return d;
}

std::ostream & operator<<(std::ostream & cout, const Deque & dq)
{
    cout << dq.get_size() << " [ ";
    for (int i = 0; i < dq.get_size(); ++i)
    {
        cout << dq[i] << " ";
    }
    cout << "]";

    return cout;
}

The operator works just fine. The bools work just fine and the remove_head and remove_tail thing I'll do once I figure out insert tail. Right now, it's not making any chances to the very object I've created inside the main.

#include <iostream>
#include "Deque.h"

void print(const Deque & deque)
{
    static int i = 1;
    std::cout << i << ". " << deque << ", empty: " << deque.is_empty()
            << ", full: " << deque.is_full();
    i++;
}

void test_insert_tail(Deque & deque, int x)
{
    deque.insert_tail(x);
    print(deque); std::cout << "\n";
}

int main()
{
    Deque deque;
    print(deque);
    std::cout << "\n";
    test_insert_tail(deque, 2);
    return 0;
}

The output should look like this, 1. 1 [ 2 ], empty: 0, full: 0 but looks like this 1. 0 [], empty: 1, full: 0

What's going on inside my static method for handling all the private attributes that I'm missing on? What did I do wrong exactly?

标签: c++oopmethodsstaticconstants

解决方案


您的代码的问题是对静态词的滥用。实际上,静态意味着不与对象的实例相关联:这意味着静态成员(array_在本例中为变量)的内容在您将创建的每个实例之间共享。

方法也是一样,insert_tail即使不创建实例也可以使用。现在,让我们试着理解你在这个方法中写了什么:

Deque d;
d.size_ += 1;
int size = d.size_;
d.array_[size - 1] = x;
return d;

在第一行中,您创建了一个新Deque对象。这是第一个错误,因为您没有修改实际的Deque. 然后添加操作,最后返回创建的 Deque。但是,此对象不会保存在任何地方,因为当您调用时,deque.insert_tail()您不会在任何地方分配返回的值。

让我们试着让这个更具体一点。

由于您正在做的是创建数据结构,因此您不需要任何静态成员。这是因为所有内容都需要保存在数据结构中。

然后,在里面insert_tail你需要删除你在里面创建的对象。它看起来像这样:

size_ += 1;
int size = size_;
array_[size - 1] = x;

通过这两个修改,代码可能会按预期工作,但是,我建议您专注于改进代码的外观。在变量名末尾使用下划线字符有点令人困惑。在 C 中,您可以在名称中将其int foo_bar用于普通变量,并在开头int _foo用于保留变量。


推荐阅读