c ++无法更改数组

时间:2015-11-02 23:39:52

标签: c++ arrays

我真的坚持以下问题:

2个班级:

class Article
{
    char* _code;
    char* _name;
    double _price;

    int _quantity; 

public:

    Article(char* code = "code", char * name = "name", double price = 0){
        setCode(code);
        setName(name);

        _price = price;
        _quantity = 0;
    }


    Article(Article & a){
        setCode(a._code);
        setName(a._name);

        _price = a._price;
        _quantity = a._quantity;
    }


    ~Article(){
        delete[] _code; _code = nullptr;
        delete[] _name; _name = nullptr;

    }

    /*******SETTERS********/

    void setCode(char* code){
        int vel = strlen(code);
        _code = new char[vel + 1];

        strncpy_s(_code, vel + 1, code, _TRUNCATE);
    }

    void setName(char* name){
        int vel = strlen(name);
        _name = new char[vel + 1];

        strncpy_s(_name, vel + 1, name, _TRUNCATE);
    }

    void setPrice(double c){ _price = c; }

    /*******GETTERS********/
    int getquantity(){ return _quantity; }
    double getprice(){ return _price; }


    //operator = 
    Article operator = (Article& a){
        setName(a._name);
        setCode(a._code);
        _price = a._price;
        _quantity = a._quantity;

        return *this;
    }


    //operator-= decrease qnty for passed value.

    bool operator-=(int v){
        if (_quantity > v)
        {
            _quantity = _quantity - v;
            return true;
        }
        cout << "Operacija nije moguca, stanje je manje nego vrijednsot za koju se umanjuje";
        return false;
    }

    // operator ++ increase Qnty for 1.
    Article operator++(int){

        //Article A(_code, _name, _price);
        this->_quantity++;
        return *this;

    }

    //Preklopiti operator -- decrease value for 1.
    Article operator--(){

        if (_quantity >= 1) 
        {
            _quantity--;
        }

        return *this;

    }

    //compare articles based on code.

    bool operator==(Article & a){
        if (strncmp(_code, a._code, strlen(_code)) == 0)
        {
            return true;
        }
        return false;
    }

    //operator <<

    friend ostream & operator<<(ostream & COUT, Article & a);


};

ostream & operator<<(ostream & COUT, Article & a){

    COUT << "code: " << a._code << endl << "name: " << a._name << endl << "price: " << a._price << ",  Qnty: " << a._quantity << "\n ----------------------- \n\n";
    return COUT;

}

class Item
{
    Article _Article;
    int* _quantity;
public:

    Item(Article & a, int quantity) :_Article(a){

        _quantity = new int(quantity);


    }

    Item() :_Article("StavkaR", "nameR", 0){

        _quantity = new int(0);


    }

    Item(Item& s) :_Article(s._Article){

        _quantity = new int(*s._quantity);

    }


    ~Item(){
        delete _quantity; _quantity = nullptr;
    }


    int getquantity(){ return *_quantity; }
    void setquantity(int v) { *_quantity += v; }

    Article getArticle() { return _Article; }

    friend ostream & operator << (ostream & COUT, Item & obj);

};

ostream & operator << (ostream & COUT, Item & obj){
    COUT << "quantity: " << *obj._quantity << endl << obj._Article;
    return COUT;
}

第一类是带有构造函数,析构和重载运算符的文章,第二类ITEM很简单,它包含文章和数量。

对于main中的测试,我有以下代码:

    Article a;
    Article b("code", "name", 1.5);
    Article c(b);

    cout << a;
    cout << b;
    c++;
    c++;
    c.setPrice(10);
    cout << c;


    cout << "\n ************************************** \n";



    Item _stavke[100];
    for (size_t i = 0; i < 3; i++)
    {
        _stavke[i].getArticle().setPrice(100);
        cout << _stavke[i];
        cout << "\n \t\t\t ---- \n";
    }

代码的第一部分,我测试Article类和一些重载的运算符,它工作正常,但第二部分,我有Item _stavke[100];数组我可以列出数组的成员 - 没有问题,但任何调用的更改不被接受基本代码 _stavke[i].getArticle().setPrice(100); 似乎没有做任何事......

有人能指出我哪里出错了,为什么我不能改变阵列的成员?

提前谢谢

1 个答案:

答案 0 :(得分:5)

你的getArticle()调用按值返回_Article。也就是说,文章的副本。您的setPrice()在副本上被调用,因此它不会反映在原始Item的文章中,该文章保持不变。