- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 C++ 实现上述系统。以前,我使用 C# 和 OOP 来编写我的程序,所以这将是我第一次使用 C++,我知道这两种语言之间存在一些差异。我想做的是计算 Logbook 类成员列表中的选民人数。
在 C# 中,我将使用
foreach(Member m in _members) {
if(Member m is Voter) {
votercount++;
}
}
但是,我不确定在cpp中,这个实现是否正确?在我的 Logbook.h 文件中
class Logbook
{
private:
std::list<Member> _members;
在我的 Logbook.cpp 文件中:
int Logbook::CandidateCount() {
int membercount;
for(Member m: _members) {
if (Member* m=dynamic_cast<const Member*>(&Candidate)) membercount++;
}
return membercount;
}
它在 &Candidate
处显示错误,其中显示标识符 Candidate 未定义。是因为Logbook类无法到达Candidate类吗?
非常感谢任何回复和帮助。
最佳答案
您在这里做错了几件事。首先,您没有初始化计数变量,因此它会使用一些随机值(可能为零或其他值)开始。
接下来,您需要将指针 存储到列表的成员中,因为在C++
中,多态性仅通过指针 起作用。如果列表负责删除它的元素(通常),那么你应该使用像 std::unique_ptr 这样的智能指针。 :
class Logbook {
public:
int CandidateCount();
// virtual destructor is (usually) important for polymorphic types
virtual ~Logbook() = default;
// store pointers in your list
std::list<std::unique_ptr<class Member>> members;
};
然后您可以遍历该列表,尝试动态地将每个指针指向您要计数的类型。如果它返回一个有效的指针,那么您就知道它属于那种类型。否则将返回一个 nullptr
:
class Member: public Logbook {};
class Candidate: public Member {};
class Voter: public Member {};
int Logbook::CandidateCount()
{
int membercount = 0; // initialize this!!!!
for(auto& m : members) { // use reference here to avoid making a copy
if(dynamic_cast<Candidate*>(m.get()))
membercount++;
}
return membercount;
}
注意:如果您想做的不仅仅是计数您的候选人,您可以保留从动态转换中获得的指针 像这样:
class Candidate: public Member { public: void do_something(){} };
int Logbook::CandidateCount()
{
int membercount = 0; // initialize this!!!!
for(auto& m : members) { // use reference here to avoid making a copy
if(auto c = dynamic_cast<Candidate*>(m.get())) {
membercount++;
// c is not nullptr and is type Candidate*
c->do_something(); // use your Candidate like this
}
}
return membercount;
}
关于c# - C++ 等同于 C# OOP if(Boy b is Student),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53029074/
我对这些术语感到困惑。 假设我们有一个 Student 类,那么 的含义是什么 class Student{ public: Student(const Student& a){ ..
嘿伙计们,我可以得到一些关于这个java项目的帮助吗我的问题是这一行Student s = new Student(r.getChar(),r.getChar(),r.getNum(),r.getNu
这个问题在这里已经有了答案: What causes error "No enclosing instance of type Foo is accessible" and how do I fix
在我的一次面试中,一位面试官问我: 给定一个 Student 类和两个对象 s1 和 s2: s1 = new Student(); s2 = new Student(); s1 == s2 如何返回
这个问题在这里已经有了答案: Why can a "private" method be accessed from a different instance? (4 个答案) 关闭 7 年前。 那
我想让我的类(class)在存储在列表中时可以排序(按年龄)。 我读到这个:IComparable Vs IComparer我让我的类(class)可排序。 public class Student
我有一个 List和 String firstName="George" .我想从与 "George" 匹配的列表中提取学生对象没有迭代列表。可能吗? 可能是需要某些方法覆盖或任何使其工作的方法。 p
我收到一个错误extraqualification 'student::' on member 'student' [-fpermissive]。 还有为什么 name::name 这样的语法会用在构
嗨,我正在为 C 类制作这个程序,我已经绞尽脑汁,我来到这里,因为我不能去我的老师或辅导部门,因为当我不工作时他们不可用。 . 我在使用这两个函数时遇到问题 void examination_seat
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
请考虑以下代码: class Student { } enum StudentType { } static void foo(IDictionary> students) { } static
What should i do?, i try many ways to solve this problem but still uncaught type error .push and .ma
每当我想在 C++ 上使用模板在二叉树上添加一个元素时,我就会遇到这个错误,而且我很难修复它, 所以这里是我的代码是 Tree.c 文件: template ostream & operator
创建数据库“student”后,当我设置该数据库的权限时,出现错误,并显示消息“错误 1146(42S02):表“student.db”不存在。我不明白为什么会发生这种错误,尽管我确实创建了数据库。
我正在尝试创建一个允许变量赋值的函数,但我不断收到错误: private static void DatabaseSelect(string ToBeSelected, string WhichTab
我认为展示一些经典的 CS 问题并让人们展示他们的算法优化技巧会很有趣。希望我们能够看到一些巧妙的技术来解决我们可以在实践中实现的抽象问题。 理想情况下,解决方案将以具有大 O 分类的伪代码呈现。这种
我是 Java 初学者,陷入了一项任务。如果有人能帮助我,我将非常感激。我有一个具有这些属性的学生类(class) - 姓名、化学分数、数学分数和物理分数。在主课中,我与几个学生创建了一个 Array
我有一个 Java 类 class Student { String name; int age; } 此外,我还有 Student 类 student1 和 student2 的两个
我正在探索 Java 8 功能以从 say Student 对象列表中删除空对象,我有以下代码 import java.util.ArrayList; import java.util.Arrays;
我正在学习 C++,我需要在 C++ 中创建结构“Student”并使用它(我使用 Microsoft Visual C++ 2010 Express) 我创建了Student.cpp文件 #incl
我是一名优秀的程序员,十分优秀!