Winten
Winten

Reputation: 664

C++ Getting an object

Lets say I have the following class:

static int counter = 0;

class Account {
public:
   int ID;
   int favNumber;

   Account(int favNum) {
      this->ID = ++counter;
      this->favNumber = favNum;
   }
};

Account user1(4);
Account user2(9);

Now both accounts user1 and user2 have different ID that is unique. Is there any way by knowing the ID of the account get the field of the object like "favNumber", if so how should it be implemented?

Something like getFieldById(int ID)

Upvotes: 0

Views: 118

Answers (4)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14530

You may use std::map to do this :

#include <map>

class Account {
   // Make attributes private. It is a better practice
   int ID;
   int favNumber;

   static int s_Counter;
 //^^^^^^^^^^^^^^^^^^^^^ It is better to move it as a static private member of Account

public:

   Account(int favNum) {
      this->ID = ++s_Counter;
      this->favNumber = favNum;
   }

   // GETTERS
   int GetFavNumber() const { return favNumber; }
   int GetID() const { return ID; }
};

int Account::s_Counter = 0;
// ^^^^^^^^^^^^^^^^^^^^^^^^ Don't forget to initialize it

Account user1(4);
Account user2(9);

std::map<int, Account*> accounts;
accounts[user1.GetID()] = &user1;
accounts[user2.GetID()] = &user2;

// To get a favNum with some id :
accounts[id]->GetFavNumber();

But with this technique, be sure that the pointers are still valid ! If not, you could have bad surprises ...


What we have done in this previous code ?

  • We passed the attributs in private (better practice).
  • We created Getters to access them.
  • We passed the counter static variable as a static private member of Account.
  • We used std::map to have a listing of the accounts created and the keys are the IDs of the Accounts.

Upvotes: 2

Karthik T
Karthik T

Reputation: 31972

You would need to central place to store all the objects that are going to be created and then search for the id there.

You could store them as

  • Plain old array
    Search the entire list for your object of ID and then return the field

  • ID indexed array
    array[ID] is the object you need, return the field

  • Hash(std::map) from ID to object
    Similar syntax as ID indexed array but is a hash table lookup

Each have their pros and cons in simplicity, speed of search, memory used etc.

You could also store object pointers in the above.

To automate things, you can make the above list a private static member of your Account class, and add to it in the constructor.

Upvotes: 0

No Idea For Name
No Idea For Name

Reputation: 11607

you can create a list and for each time you pass the constructor add the item to the list. then when a request get to your getFieldById search your list.

the list will have to be in a place you can search in and only be initiate once

Upvotes: 0

Neil Kirk
Neil Kirk

Reputation: 21813

You can use

std::map<int, Account*>

to store a pointer to the accounts by their id. It's up to you to make sure the pointers remain valid. Alternatively, you could use

std::map<int, Account>

and let the map look after your accounts for you.

Upvotes: 0

Related Questions