Need help with pointers

Ok, I'm learning C++ through a book I found online (Fundamentals of C+ Programming by Richard L. Halterman). After each chapter there is some exercises.

The exercise I need help with is as following:

3. The following C++ program is split up over three source files. The first file, counter.h, consists of

1
2
3
4
5
// counter.h

int read();
int increment();
int decrement();


The second file, counter.cpp, contains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// counter.cpp

static int count;

int read() {
   return count;
}

int increment() {
   if (count < 5)
      count++;
}

int decrement() {
   if (count > 0)
      count--;
}


The third file, main.cpp, is incomplete:

1
2
3
4
5
6
7
8
9
// main.cpp

#include <iostream>
#include "counter.h"
using namespace std;

int main() {
// Add code here
}


(a) Add statements to main that enable it to produce the following output:


3
2
4


The restriction is that the only output statement you are allowed to use (three times) is

 
cout << read() << endl;


(b) Under the restriction of using the same output statement above, what code could you add to main so that it would produce the following output?


6



Ok, so (a) is pretty easy. But I need a little help with (b)...
The chapter with this exercise was about global variables, static variables, header files, and pointers at last. So I guess I need to use pointers in some way..
I've tried a couple of different things, but non of them worked..
Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// My main.cpp file

#include <iostream>
#include "counter.h"
using namespace std;

int main()
{
    // Print out 3
    increment();
    increment();
    increment();
    cout << read() << endl;

    // Print out 2
    decrement();
    cout << read() << endl;

    // Print out 4
    increment();
    increment();
    cout << read() << endl;

    // Print out 6
    // What to do here??
}


Of course it prints out:


3
2
4


So.. Please help :)
And thank you in advance!
Did you figure this out yet? I'd like to know the answer here too. I've come up with three different ways to do
it (well, four if you count undefined behaviour), but I don't think any of them is what the author had in mind...

#1 Use a macro:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "counter.h"
using namespace std;

int main()
{
    /* ... */

    // Print out 6

    #define read() 6

    cout << read() << endl;
}

#2 Use a C++11 lambda:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "counter.h"
using namespace std;

int main()
{
    /* ... */

    // Print out 6

    auto read = []() { return 6; };

    cout << read() << endl;
}

#3 Use a C++03 lambda:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "counter.h"
using namespace std;

int main()
{
    /* ... */

    // Print out 6

    struct {
        int operator ()() {
            return 6;
        }
    } read;

    cout << read() << endl;
}

#4 Rely on undefined behaviour [1]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include "counter.h"
using namespace std;

static int count;

inline int read() {
   return count;
}

inline int increment() {
   //if (count < 5)
      count++;
}

inline int decrement() {
   if (count > 0)
      count--;
}

int main()
{
    // Print out 3
    increment();
    increment();
    increment();
    cout << read() << endl;

    // Print out 2
    decrement();
    cout << read() << endl;

    // Print out 4
    increment();
    increment();
    cout << read() << endl;

    // Print out 6
    increment();
    increment();
    cout << read() << endl;
}

[1] http://stackoverflow.com/questions/6843202/what-if-redefine-an-inline-function
Ok, I think I got it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include "counter.h"
using namespace std;

static int count;

int myRead() {
   return count;
}

int myIncrement() {
   //if (count < 5)
      count++;
}

int myDecrement() {
   if (count > 0)
      count--;
}

typedef int(*myFunction_type)();

int main()
{
    myFunction_type read      = myRead;
    myFunction_type increment = myIncrement;
    myFunction_type decrement = myDecrement;

    // Print out 3
    increment();
    increment();
    increment();
    cout << read() << endl;

    // Print out 2
    decrement();
    cout << read() << endl;

    // Print out 4
    increment();
    increment();
    cout << read() << endl;

    // Print out 6
    increment();
    increment();
    cout << read() << endl;
}

I don't know why I didn't think of this earlier... >_>
Topic archived. No new replies allowed.