2
class AccountManager
{
private:
    Account accountlist[100];
    int *accountNumber;
    Account* SuperVipAccount; 
    static int ManagerNumber;
public
    int getManagerNumber() const;
};

I have a class like this, and I want to use decrement operator in "getManagerNumber" to let ManagerNumber minus one, what should I do?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
lpy
  • 587
  • 1
  • 6
  • 20

2 Answers2

4

ManagerNumber is a static member of the AccountManager (shared across the class and not per object), so you can decrement it very well.
Method's const correctness doesn't apply to the static members.

int getManagerNumber() const
{
  -- ManagerNumber;  // ok
  return ManagerNumber;
}
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • AccountManager::AccountManager() { ManagerNumber = 0; this -> SuperVipAccount = NULL; *(this -> accountNumber) = 0; } I got "undefined reference to `AccountManager::ManagerNumber'" – lpy Mar 20 '12 at 08:54
  • 1
    @Laurent `static int ManagerNumber` declares the variable but doesn't define it. Put `int AccountManager::ManagerNumber = 0;`, or whatever the initial value is going to be, in the cpp file. I usually put it near the top, after the includes but before the function definitions. – Peter Wood Mar 20 '12 at 09:03
  • @Laurent: if you do "*(this -> accountNumber) = 0;" without initializing your pointer, then you have undefined behavior (next to what littleadv already remarked) – stefaanv Mar 20 '12 at 09:04
0
class AccountManager
{
static int ManagerNumber;
}

AccountManager::ManagerNumber=0;

 int AccountManager::getManagerNumber()
    {
    return --ManagerNumber;
    }
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112