虚函数不适用于1类,但适用于另一类

时间:2015-07-24 17:26:45

标签: c++

我的程序具有以下结构:类核心是基类,类 Grad 继承Core,类 Pass_fail 继承Core。类 Student_info 只管理指向这些类的指针,并根据用户的需要在运行时创建适当的对象。问题:类Core,Grad和Pass_fail有一个名为grade()的虚函数。每个班级都以自己的方式计算成绩。当程序使用Core或Grad对象调用grade()时,它可以正常工作。但是当程序调用pass_fail obj的grade()时,虚拟机制不起作用,而是调用Core.grade()函数。

Class Core:

class Core {
    friend class Student_info;
public:
    Core() : midterm(0), final(0) { std::cerr << "Core obj created" << std::endl; }
    Core(std::istream& is);

    std::string name() const;

    virtual std::istream& read(std::istream&);
    virtual double grade() const;
    virtual bool valid() const;
    virtual bool did_req() const;

    virtual ~Core() {  }
protected:
    virtual Core* clone() const { return new Core(*this); }
    std::istream& read_common(std::istream&);
    std::istream& read_hw(std::istream&, std::vector<double>&);
    double midterm, final;
    std::vector<double> homework;
private:
    std::string n;
};

Class Grad:

class Grad : public Core {
public:
    Grad() : thesis(0) { std::cerr << "Grad obj created" << std::endl; }
    Grad(std::istream& is);

    double grade() const;
    std::istream& read(std::istream&);
    bool did_req() const;
protected:
    Grad* clone() const { return new Grad(*this); }
private:
    double thesis;
};

Class Pass_fail:

class Pass_fail : public Core
{
public:
    Pass_fail() { std::cerr << "Pass_fail obj created" << std::endl; }
    Pass_fail(std::istream&);
    std::istream& read(std::istream&);

    double grade() const;
    //bool did_req() const;
protected:
    Pass_fail* clone() { return new Pass_fail(*this); }
};

Pass_fail等级功能:

double Pass_fail::grade() const
{
    std::cerr << "Pass_fail grade() function" << std::endl;
    if (valid())
        return Core::grade();
    else
    {
        std::cerr << "Homework is empty" << std::endl;
        return (midterm + final) / 2.0;
    }
}

Class Student_info(管理指针):

class Student_info {
public:
    Student_info(): cp(0) { }
    Student_info(std::istream& is) : cp(0) { read(is); }
    Student_info(const Student_info&);
    Student_info& operator=(const Student_info&);
    ~Student_info() { delete cp; }

    std::istream& read(std::istream&);
    bool valid() { return cp->valid(); }
    bool did_req() { return cp->did_req(); }

    std::string name() const
    {
        if (cp) return cp->name();
        else throw std::runtime_error("uninitialized student");
    }
    double grade() const
    {
        if (cp) return cp->grade();
        else throw std::runtime_error("uninitialized student");
    }
    static bool compare(const Student_info& s1, const Student_info& s2)
    {
        return s1.name() < s2.name();
    }

private:
    Core* cp;
};

一段main():

...
vector<Student_info> students;
    Student_info record;
...
...
while (record.read(cin))
    {

        students.push_back(record);
    }
....
double final_grade = students[i].grade(); \\ the problem is here-> Core.grade() func is called instead of Pass_fail.grade()

1 个答案:

答案 0 :(得分:0)

我在clone()中发现错误const不是Pass_fail,这就是虚拟机制无效的原因。

每次我Student_info时,

students.push_back(record)复制构造函数都会产生。由于clone()类中没有覆盖Pass_fail,因此产生了错误的clone(),因此错误grade()