指向对象的指针的私有矢量.如何使用getter方法访问这些对象
A private vector of pointers pointing to objects. How to access those objects with getter method?
我的程序是一个循环的六次迭代,其中八个人相互投票。每个人在每次迭代期间投票给谁被保存到私有类成员voteList
(指针的向量(。
我的问题是,在六次迭代结束时,我希望能够使用我编写的GetVote(int)
公共方法,说出Anna在每次投票中投票给了谁。
我认为*(voteList[round])
应该是安娜在某一轮投票中投票给谁的价值(一个人(?并且使用GetName()
方法应该检索那个人的名字的字符串。但无论我如何摆弄它,每当我调用GetVote()
时,程序都会崩溃。
我确信我犯了一个或多个非常愚蠢的错误,但我不知道问题出在哪里。任何意见都将不胜感激!
#include <iostream>
#include <vector>
#include <random>
#include <time.h>
using namespace std;
enum gender { male, female };
class Person {
private:
string personName;
gender personGender;
vector<Person *> voteList;
public:
// Constructors
Person (string, gender);
// Setters
void Vote (Person * target) {
voteList.push_back (target);
};
// Getters
string GetName () { return personName; };
string GetVote (int round)
{
Person ugh = *(voteList[round]);
return ugh.GetName ();
};
};
Person::Person (string a, gender b) {
personName = a;
personGender = b; }
void Voting (vector<Person> voters)
{
for (int i = 0; i < voters.size(); i++) {
int number = (rand() % voters.size());
Person * myTarget = &voters[number];
voters[i].Vote (myTarget);
cout << voters[i].GetName() << " votes for " << voters[number].GetName() << endl;
}
cout << endl;
}
int main()
{
srand(time(0));
Person Anna ("Anna", female);
Person Baxter ("Baxter", male);
Person Caroline ("Caroline", female);
Person David ("David", male);
Person Erin ("Erin", female);
Person Frank ("Frank", male);
Person Gemma ("Gemma", female);
Person Hassan ("Hassan", male);
vector<Person> theGroup;
theGroup.push_back (Anna);
theGroup.push_back (Baxter);
theGroup.push_back (Caroline);
theGroup.push_back (David);
theGroup.push_back (Erin);
theGroup.push_back (Frank);
theGroup.push_back (Gemma);
theGroup.push_back (Hassan);
for (int n = 0, iterations = (theGroup.size() - 2); n <= iterations; n++)
Voting (theGroup);
cout << "ANNA VOTED FOR...";
for (int n = 0; n <= 5; n++)
{
cout << "Round " << (n + 1) << ": " << Anna.GetVote(n) << 'n';
}
cin.ignore();
return 0;
}
当您调用voting
时,您传递向量的副本,内容也将被复制。
您应该将此矢量作为参考:
void Voting (vector<Person>& voters) { ... }
您可能还想在GetVote
中添加一些安全检查,以确保调用方不会提供超出范围的索引。
首先,您要在各处复制Person
对象。例如,将Person
对象添加到theGroup
向量时,以及再次将同一向量传递到Voting
函数时。
复制人在语义上没有任何意义。为了避免这种情况,您应该在Person
类中添加一个私有复制构造函数和赋值运算符:
private:
Person(const Person& other);
Person& operator=(const Person& rhs);
接下来,您将不得不更改向量以使用Person
指针:
vector<Person *> theGroup;
theGroup.push_back (&Anna);
theGroup.push_back (&Baxter);
theGroup.push_back (&Caroline);
theGroup.push_back (&David);
theGroup.push_back (&Erin);
theGroup.push_back (&Frank);
theGroup.push_back (&Gemma);
theGroup.push_back (&Hassan);
您可以使用->
运算符在指向对象的指针上调用方法,例如:
string GetVote (int round)
{
Person *ugh = voteList[round];
return ugh->GetName ();
};
和:
void Voting (const vector<Person *>& voters)
{
for (int i = 0; i < voters.size(); i++) {
int number = (rand() % voters.size());
Person *myTarget = voters[number];
voters[i]->Vote (myTarget);
cout << voters[i]->GetName() << " votes for " << voters[number]->GetName() << endl;
}
cout << endl;
}
void Voting (vector<Person> voters)
你想把它作为参考。否则,当你得到voters[number]
的地址时,你就得到了函数的一个局部变量的地址——一旦你尝试实际使用它,一切都会变得疯狂
void Voting (vector<Person> &voters)
事实上,这并不完全是问题所在,尽管这仍然与参考文献有关。您通过副本传递原始vector
,这意味着原始vector
(在main
函数中(不会被函数的操作修改(当然,它的内容也是如此(。因此,里面的所有Person
都有它们的原始状态,只有一个空的voteList
vector
。显然,如果你试图取消引用它的任何(不存在的(元素,那就不太好了!
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 此getter是否返回由构造函数或对班内对象的引用给出的引用?C
- 专用对象"getter setter"特定对象
- 为实例变量(即对象)调用getter函数时实际发生的情况
- C++ - 通过 getter 函数在单独的类中从对象指针映射访问成员函数
- 无法从向量访问对象的getter
- 在没有公共getter的情况下,比较两个相同类型的对象是否相等
- 在调用右值对象的getter方法时获取右值
- 返回其他类对象的c++类getter中的错误
- 指向对象的指针的私有矢量.如何使用getter方法访问这些对象
- 代理对象/引用getter与setter
- 为对象做getter
- 找不到对象时不会崩溃的 getter 函数
- 如何让getter返回另一个对象的指针
- std::map 中的对象指针在通过 getter 检索时损坏
- c++中堆对象getter / setter的更好实践
- 为什么要继续对不可变对象使用getter呢?
- 返回带有const getter函数的成员对象,但它是可修改的