0

I'm trying to figure out how to use a getter function with a particular object that is contained within a vector that is in a separate class. Ok, there's a class Patron that contains GetNumBooks(), and there's a class PatronList that contains vector patrons, how would I within class Loan call GetNumBooks(for a patron object within vector patrons) in a conditional?

Loan class: https://pastebin.com/1KSKFvmS Patron class: https://pastebin.com/F7amyfHb PatronList class: https://pastebin.com/8ARsHQQy

I've tried the following:

in PatronList:

vector<Patron>* ReturnVec(){
       vector<Patron>* PatPtr = new vector<Patron>;
        PatPtr = &patrons;
       return PatPtr;
       
      }

in Loan:

void Loan::Test(Patron myPatron){
 vector<Patron>* here = new vector<Patron>;
  here = list.ReturnVec();
  here->push_back(myPatron);
  here->at(0).SetNumBooks(11);
  cout << here->at(0).GetNumBooks();
}

Which seems to work but...I suppose I want to know if there's a better solution. Are pointers even necessary? If so, do I really need to declare a pointer in both Test and ReturnVec?

Marian Klösler
  • 620
  • 8
  • 22
B00m23
  • 1
  • 1
  • BTW, you don't need to use raw-pointers and `new vector` here - [you can allocate it locally and return it by-value](https://stackoverflow.com/questions/15704565/efficient-way-to-return-a-stdvector-in-c) - in fact, you should entirely avoid using raw pointers: C++ is not C. – Dai Apr 19 '23 at 10:30

0 Answers0