gpt4 book ai didi

c++ - void* 不是指针对象/转换实现

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:11 26 4
gpt4 key购买 nike

根据我下面的代码,我正在尝试创建一个类(class)评分系统。因此,它将存储作业的类型、权重和取得的成绩。我试图让它打印原始分数(这是输入的等级,例如:“B-”或 85.50;取决于它是考试还是测验)。

我已经阅读了关于“转换”的内容,它应该可以解决使用 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;
}

最佳答案

与其编写一个返回 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;
}

关于c++ - void* 不是指针对象/转换实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com