简体   繁体   中英

(C++) How can I keep track of how many objects of a class I declare?

Let's say I have a class like this one, and I want to keep track of how many employees there are and who they are using an account number system. I want to know if this is a dumb idea or if this is good, just not (so far) implemented correctly.

class Employee {
private:
  long int accountNumber;
  static int currentTotal;
  static int lifetimeTotal;
  /* other member variables like name */
public:
  /* member functions, constructors, etc. */
};

My thoughts were:

Employee joins the company

I could declare an employee, and have ++currentTotal; ++lifetimeTotal; ++currentTotal; ++lifetimeTotal; in the constructor(s). This increments the number of employees by one.

Employee leaves the company

When someone no longer works at the company, I could somehow use --currentTotal; . The current number of employees has been decreased by 1, so the variable should too; however, lifetimeTotal does not decrement.

How exactly would I decrement this? Should I do it using some kind of dummy employee and write a member function for it? Or perhaps use pointers for all the objects I declare, so I can delete them?

Employee account numbers

I was thinking that the account numbers could be generated semi-randomly, where part of the sequence of digits encodes some kind of functional information (what branch they work at, whether they receive wages or salary, etc), another part is determined by RNG, and the final digit is a check digit. I want to, however, have a way to ensure that an account number cannot be re-used. I don't know how to achieve this.

Keeping track

How exactly can I keep track of them as they come and go? My first thought was a vector or some kind of ADT (both of which I don't completely understand how to implement yet but I get the basic idea). But if I need to, say, find John Doe's salary, I need to be able to search for it among the accounts of potentially thousands of other employees (in my simulation exercise I'm just going to have a few dozen).

I really don't know much about classes/structs beyond the basics, because up until now, all I have experience with in using them is inheritance, virtual functions, const_cast and static_cast , operator overloading, friend functions, and this thing I found online called smart pointers, where instead of declaring Employee worker1( /*... */ ); , I can declare ptr<Employee> worker1(new Employee( /*... */ )); . There's so much stuff I see in examples that I don't know how to do.

I'm almost certain that an array is not the way to go, because it's of a fixed size, so I was thinking a vector is better. I really don't know what I'm doing and I would very much appreciate any help.

Depends a bit on what you want to achieve:

a. Keep track of how many objects of a specific class (here: employee ) are currently instantiated. You would implement a static member counter that's incremented in all constructors and decremented in the destructor.

b. Keep track of how many employees your company has (which is an entirely different thing as (a), think about it a bit). Here, the counter should be part of your data model (You probably have a company class that would keep track of that through its hire() and fire() methods)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM