首页 > 解决方案 > Confused about passing argument via custom functional object

问题描述

For now, I'm trying to make a little graphic class from custom class with two fields:

struct Prog {
    Prog(int y, const string& s) :yy{ y }, name{ s }{}
    Prog() :yy{0}, name{"none"} {}
    int yy;
    string name;
};

Then I create my functional object, to convert argument to coordinates value:

class Scale {
public:
    Scale() : cbase{ 40 }, vbase{ 1960 }, scale{ 20 }{};
    int operator()(const int y) {
        return cbase + (y - vbase) * scale;
    }
private:
    int cbase;
    int vbase;
    double scale;
};

And this is my Graphic class:

class Prog_shape :public Shape {
public:
    Prog_shape(const Prog& p) :txt{ {sc(p.yy),240},p.name }, cir{ {sc(p.yy),244},10 } { }
    void draw_lines() const {
        txt.draw();
        cir.draw();
    }
private:
    Graph_lib::Text txt;
    Scale sc;
    Graph_lib::Circle cir;
};

And the function that is filling the Shapes container from vector<Prog>:

void generate_figures() {
    for (auto p : p_vec) {
        sh_vec.push_back(new Prog_shape{ p});
    }
}

And now I'm really confused about my Prog_shape class's txt object, I can't get the value from the Scale func object, while the cir object can. When I take a few moments to debug, I find values passed to the txt constructor are 0. How can it be possible when the values must be the same?

标签: c++class

解决方案


推荐阅读