PairArr :: operator =(PairArr(ValArrInt(ytemp,NY),ValArrInt(btemp,NY)));

时间:2018-04-04 10:05:15

标签: c++

template <typename T1,typename T2>
class Pair
{
private:
    T1 a;
    T2 b;
public:
T1 & first() { return a; }
T2 & second() { return b; }
T1 first() const { return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval) : a(aval), b(aval) {};
Pair() {}
};

using ValArrInt = std::valarray<int>;
using PairArr = Pair<ValArrInt, ValArrInt>;

class Wine : private string, private PairArr
{
private:
    int NY;
public:
    Wine(const char * l, int y, const int yr[], const int bot[]);
    void Show() const;
};

Wine::Wine(const char * l, int y, const int yr[], const int bot[])
    :string(l), NY(y)
{
    PairArr::operator=(PairArr(ValArrInt(yr, NY), ValArrInt(bot, NY)));
}

void Wine::Show() const
{
    cout << "Wine: " << (const string &)*this << endl;
    cout << "\tYear\tBottles\n";
    for (int i = 0; i < NY; i++)
    {
        cout << "\t" << PairArr::first()[i] << "\t"
            << PairArr::second()[i] << endl;
    }
}

在这一行:

PairArr::operator=(PairArr(ValArrInt(yr, NY), ValArrInt(bot, NY)));

为什么PairArr组件最终以ValArrInt(yr, NY)(第一个参数)为a和b成员。

经过测试:

    const int YRS = 3;
    int y[YRS] = { 1993,1995,1998 };
    int b[YRS] = { 48,60,72 };
    Wine more("Gushing Grap Red", YRS, y, b);
    more.Show();

题外话: 继续要求我添加更多代码数量的详细信息......但是没有。 是否有某种比例的代码与我应该满足的非代码?

1 个答案:

答案 0 :(得分:1)

所以,显然你有印刷错误,你在两个地方输入了aval

Pair(const T1 & aval, const T2 & bval) : a(aval), b(aval) {};