void *不是指针对象/强制转换实现

时间:2016-05-17 17:05:52

标签: c++ c++11 casting void-pointers

从下面的代码中我尝试创建一个课程评分系统。因此,它将存储任务类型,权重和达到的等级。我试图让它打印原始分数(例如输入的等级:" B - "或85.50;取决于它是考试还是测验)。

我读过&#34; cast&#34;这应该解决使用void*作为指针的问题,但我仍然不清楚如何实现它。我尝试static_cast<struct Grading*>(c.gs[0])(在代码中注释掉)但它仍然返回值的地址,而不是值本身。任何帮助将不胜感激。提前谢谢!

#include <iostream>
#include <vector>
using namespace std;

struct Grading{
    string name;
    int percentage;
    virtual ~Grading(){}
    virtual void* get_raw_score(){return 0;}
    void* get_adj_score(){return 0;}
};

struct Quiz:public Grading{
    string letter_grade;
    Quiz(const string title, const int weight, const string grade){
        name=title;
        percentage=weight;
        letter_grade=grade;
    }
    virtual void* get_raw_score(){return &letter_grade;}
};

struct Exam:public Grading{
    double *score = new double;
    Exam(const string title, const int weight, const double grade){
        name=title;
        percentage=weight;
        *score=grade;
    }
    virtual void* get_raw_score(){return &score;}
};

struct Project:public Grading{
    string letter_grade;
    Project(const string title, const int weight, const string grade){
        name=title;
        percentage=weight;
        letter_grade=grade;
    }
    virtual void* get_raw_score(){return &letter_grade;}
};

struct CourseWork{
    vector<Grading*> gs;
    void push_back(Grading* g){
        gs.push_back(g);
    }
    void sort_name(){}
    void sort_score(){}
};

ostream& operator<<(ostream& o,const CourseWork c){ //output the raw score here.
 //static_cast<struct Grading*>(c.gs[0]);
    o<<c.gs[0]->name<<endl<<c.gs[0]->percentage<<c.gs[0]->get_raw_score()<<endl;
    return o;
}

int main() {

    CourseWork c;

    c.push_back(new Quiz("Quiz", 5, "B-"));
    c.push_back(new Quiz("Quiz", 5, "C+"));
    c.push_back(new Quiz("Quiz", 5, "A"));
    c.push_back(new Exam("Midterm", 10, 50));
    c.push_back(new Exam("Final", 30, 85.5));
    c.push_back(new Project("Project", 5, "A-"));
    c.push_back(new Project("Project", 15, "B-"));
    c.push_back(new Project("Project", 15, "B-"));
    c.push_back(new Project("Demo", 10, "C"));

    cout << "** Showing populated data..." << endl;
    cout << c << endl << endl;;

    c.sort_name();
    cout << "** Showing sorted by name..." << endl;
    cout << c << endl << endl;

    c.sort_score();
    cout << "** Showing sorted by score..." << endl;
    cout << c << endl;

    return 0;
}

3 个答案:

答案 0 :(得分:0)

不是编写一个返回void*的函数并试图找出它实际返回的函数,而是可以使函数在所有情况下都返回一个字符串,但是当它是一个数字时,只需将数字转换为字符串即可。你可以使用:

struct Grading{
    string name;
    int percentage;
    virtual ~Grading(){}
    virtual std::string get_raw_score() = 0; //make pure virtual since we only use Grading as a base class
    void* get_adj_score(){return 0;}
};

struct Quiz:public Grading{
    string letter_grade;
    Quiz(const string title, const int weight, const string grade){
        name=title;
        percentage=weight;
        letter_grade=grade;
    }
    virtual std::string get_raw_score(){return letter_grade;}
};

struct Exam:public Grading{
    double score; // this does not need to be a pointer
    Exam(const string title, const int weight, const double grade){
        name=title;
        percentage=weight;
        score=grade; // no more dereference
    }
    virtual std::string get_raw_score(){return std::stod(score);} // now the score is represented as a string
};

struct Project:public Grading{
    string letter_grade;
    Project(const string title, const int weight, const string grade){
        name=title;
        percentage=weight;
        letter_grade=grade;
    }
    virtual std::string get_raw_score(){return letter_grade;}
};

struct CourseWork{
    vector<Grading*> gs;
    void push_back(Grading* g){
        gs.push_back(g);
    }
    void sort_name(){}
    void sort_score(){}
};

ostream& operator<<(ostream& o,const CourseWork c){ //output the raw score here.
 //static_cast<struct Grading*>(c.gs[0]);
    o<<c.gs[0]->name<<endl<<c.gs[0]->percentage<<c.gs[0]->get_raw_score()<<endl;
    return o;
}

答案 1 :(得分:0)

您可以使您的基类抽象化并使用模板参数而不是使用void*指针:

struct IGrading {
     virtual const std::string get_raw_score() const = 0;
};

template<typename T>
class Grading : public IGrading {
public:
     virtual ~Grading() {}
};

std::ostream& operator<<(std::ostream& os, const IGrading& g) {
    os << g.get_raw_score();
    return os;
}

并根据需要派生您的实现以实现接口契约

class Quiz : public Grading<std::string> {
public:
     Quiz(std::string title, int weight, std::string rawScore) 
     : title_(title)
     , weight_(weight).
     , rawScore_(rawScore) {}
     virtual const std::string& get_raw_score() const {
         return rawScore_;
     }

private:
     std::string title_;
     int weight_;
     std::string rawScore_;
};

......和类似的实施:

class Exam : public Grading<double> {
public:
     Exam(std::string title, int weight, double rawScore) 
     : title_(title)
     , weight_(weight)
     , rawScore_(rawScore) {}
     virtual const std::string& get_raw_score() const {
         return std::to_string(rawScore_);
     }

private:
     double rawScore_;
};

或者具有数字模板参数的通用基类实现:

template<typename T>
class NumericGrading : public Grading<T> {
protected:
    NumericGrading(T rawScore) : rawScore_(rawScore) {}
public:
     virtual const std::string& get_raw_score() const {
         return std::to_string(rawScore_);
     }

protected:
     T rawScore_;
};

Exam得出:

class Exam : public NumericGrading<double> {
public:
     Exam(std::string title, int weight, double rawScore) 
     : NumericGrading<double>(rawScore)
     , title_(title)
     , weight_(weight) {}
};

这将保持您的代码类型安全,避免任何奇怪且容易出错的转换操作。

CourseWork容器可以使用抽象接口:

struct CourseWork{
    vector<IGrading*> gs;
    void push_back(IGrading* g){
        gs.push_back(g);
    }
    void sort_name(){}
    void sort_score(){}
};

答案 2 :(得分:-1)

略有不同的方法,受Java影响:你有两种不同的成绩,一种是数字(85%),一种是字母(B - )。

无论如何输入成绩,如果你需要用它来计算,你不能使用“85%”或“B-”,你必须达到0.85。

因此,您的抽象Grade类需要提供两种方法:

std::string asString();
double asNumber();

实现可以有不同的构造函数,它们接受指定成绩的不同方法。