1

This seems like bad coding practice, but it was the guidelines given to me. I want to preface by saying I have no idea how this will actually be implemented in the main, Im just writing the class.

class Example
{
  private:
     static int total;
  public:
    void initalizeTotal();
}

Example::initalizeTotal()
{
   total = 0;
}

total will (I guess) be used to count the number of objects of that class. This is basically what I'm getting at. The problem is how I figure out how to actually call the function. I can't just call it in the constructor, sense that would reset total each time. And I've tried and failed "check" if the variable has a value yet and if not, call the function.

Is there any advice anyone can give?

EDIT: I forgot to include that total is static. And that i MUST have a function that initializes total. Thats a requirement of the assignment.

user2036101
  • 75
  • 2
  • 8

4 Answers4

5

Since total should be the same variable for every object, you should make it static:

class Example {
    private:
        static int total;
}

To initialize a static variable, you can place this line in a .cpp file:

int Example::total = 0;

You do not need to call any function to do this initialization.

David G
  • 94,763
  • 41
  • 167
  • 253
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

You can make total variable static:

class Example
{
  private:
     void addObject();
  public:
     static int total;
     Example();
}

Example::Example()
{
   addObject();
}

void Example::addObject()
{
   total++;
}

So that it will not belong to any specific object. If you then increase it's value in addObject() method which will be called in a constructor, you will get objects count.

To access it, you will not be using any Example object, but instead you may use it like this:

std::cout << "Objects count:" << Example::total;

If you want to initialize it, you do it same way somewhere in your code:

Example::total = 0;
Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
  • 1
    The name should change as well, it's not initializing anything. – Ed S. Feb 02 '13 at 22:17
  • You're missing a return type on your definition of `initalizeTotal()` now. Since it doesn't touch the state of any instance it should be `static` also. You'd be better off making it private too otherwise everyone and anyone can mess with what seems to be internal state. – Flexo Feb 02 '13 at 22:18
  • 1
    Also, and I realize this is going beyond the scope of the question, this will break if these objects are ever created simultaneously by multiple threads. – Ed S. Feb 02 '13 at 22:19
2

You have to use 'total' field as static variable in order to share with all created objects of type Example. And any time instantating a new object increase the total field. Hope this help

Nick
  • 4,192
  • 1
  • 19
  • 30
1

Try the following:

#include <iostream>

class Example {
   static int total;

   public:
     Example() { total++; }
     static int getTotal() { return total; }
};

int Example::total = 0;

int main() {
    Example a, b, c, d;

    std::cout << Example::getTotal(); // 4
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • Why is `getTotal()` not `static`? – Flexo Feb 02 '13 at 22:21
  • 1
    It should be because it accesses no non-static state and has no need of such access, for the same reason that non-mutating members should be `const`. You [can still call `a.getTotal()` even if it's static](http://stackoverflow.com/a/325572/168175) though, just like calling a `const` member on a non-const instance. – Flexo Feb 02 '13 at 22:26