Re: class variables: scoping
"Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote in message:
[color=blue]
> Of course, comments are a nice idea too![/color]
I don't understand exactly how usnig functors could
help in this situation. However I have come up with
another solution that solves the problem which I
originally posted. Essentially I achieve the desired
result and feature using an std::map which
associates with each instance the desired variable.
Hence using static local variables associated with
instances achieves the effect of instance specific
local variables, as originally requested. Here we
go. Finally, something that seems to work neatly! I
was getting quite tired of inspecting my class
variables to see whether I don't have some variable
which is not being used by any method. Cluttering
all my function specific methods was simply
unstylish IMHO.
Comments on the solution posted below very welcome!
Regards,
Neil
#include <iostream>
#include <cstdlib>
#include <map>
class Foo {
public:
int foo1() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo1..." << std::endl;
x1 = compute(0, 0);
computed[this] = true;
} else
std::cout << "Reusing computed foo1 result..." << std::endl;
return x1;
}
int foo2() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo2..." << std::endl;
x2 = compute(0, 1);
computed[this] = true;
} else
std::cout << "Reusing computed foo2 result..." << std::endl;
return x2;
}
int foo3() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo3..." << std::endl;
x3 = compute(1, 0);
computed[this] = true;
} else
std::cout << "Reusing computed foo3 result..." << std::endl;
return x3;
}
int foo4() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo4..." << std::endl;
x4 = compute(1, 1);
computed[this] = true;
} else
std::cout << "Reusing computed foo4 result..." << std::endl;
return x4;
}
private:
int compute(int a, int b) {
/* suppose this takes a long time to run */
return rand() + 2 * a + b; /* for simplicity */
}
int x1, x2, x3, x4; /* data to be computed */
};
int main() {
/* run program without waiting for unnecessary computations */
/* available which would unnecessarily initialize class data */
std::cout << "Foo 1 Instance:" << std::endl;
Foo foo1;
std::cout << foo1.foo1() << std::endl;
std::cout << foo1.foo1() << std::endl;
std::cout << foo1.foo2() << std::endl;
/* foo1.foo3(), and foo1.foo4() never called */
/* hence OO program can run as fast as a procedural program */
std::cout << "Foo 2 Instance: " << std::endl;
Foo foo2;
std::cout << foo2.foo2() << std::endl;
std::cout << foo2.foo3() << std::endl;
std::cout << foo2.foo4() << std::endl;
std::cout << foo2.foo4() << std::endl;
/* foo1.foo1() never called */
/* hence OO program can run as fast as a procedural program */
}
Output:
Foo 1 Instance:
Computing foo1...
1804289383
Reusing computed foo1 result...
1804289383
Computing foo2...
846930887
Foo 2 Instance:
Computing foo2...
1681692778
Computing foo3...
1714636917
Computing foo4...
1957747796
Reusing computed foo4 result...
1957747796
"Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote in message:
[color=blue]
> Of course, comments are a nice idea too![/color]
I don't understand exactly how usnig functors could
help in this situation. However I have come up with
another solution that solves the problem which I
originally posted. Essentially I achieve the desired
result and feature using an std::map which
associates with each instance the desired variable.
Hence using static local variables associated with
instances achieves the effect of instance specific
local variables, as originally requested. Here we
go. Finally, something that seems to work neatly! I
was getting quite tired of inspecting my class
variables to see whether I don't have some variable
which is not being used by any method. Cluttering
all my function specific methods was simply
unstylish IMHO.
Comments on the solution posted below very welcome!
Regards,
Neil
#include <iostream>
#include <cstdlib>
#include <map>
class Foo {
public:
int foo1() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo1..." << std::endl;
x1 = compute(0, 0);
computed[this] = true;
} else
std::cout << "Reusing computed foo1 result..." << std::endl;
return x1;
}
int foo2() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo2..." << std::endl;
x2 = compute(0, 1);
computed[this] = true;
} else
std::cout << "Reusing computed foo2 result..." << std::endl;
return x2;
}
int foo3() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo3..." << std::endl;
x3 = compute(1, 0);
computed[this] = true;
} else
std::cout << "Reusing computed foo3 result..." << std::endl;
return x3;
}
int foo4() {
static std::map<Foo *, bool> computed;
if (!computed[this]) {
std::cout << "Computing foo4..." << std::endl;
x4 = compute(1, 1);
computed[this] = true;
} else
std::cout << "Reusing computed foo4 result..." << std::endl;
return x4;
}
private:
int compute(int a, int b) {
/* suppose this takes a long time to run */
return rand() + 2 * a + b; /* for simplicity */
}
int x1, x2, x3, x4; /* data to be computed */
};
int main() {
/* run program without waiting for unnecessary computations */
/* available which would unnecessarily initialize class data */
std::cout << "Foo 1 Instance:" << std::endl;
Foo foo1;
std::cout << foo1.foo1() << std::endl;
std::cout << foo1.foo1() << std::endl;
std::cout << foo1.foo2() << std::endl;
/* foo1.foo3(), and foo1.foo4() never called */
/* hence OO program can run as fast as a procedural program */
std::cout << "Foo 2 Instance: " << std::endl;
Foo foo2;
std::cout << foo2.foo2() << std::endl;
std::cout << foo2.foo3() << std::endl;
std::cout << foo2.foo4() << std::endl;
std::cout << foo2.foo4() << std::endl;
/* foo1.foo1() never called */
/* hence OO program can run as fast as a procedural program */
}
Output:
Foo 1 Instance:
Computing foo1...
1804289383
Reusing computed foo1 result...
1804289383
Computing foo2...
846930887
Foo 2 Instance:
Computing foo2...
1681692778
Computing foo3...
1714636917
Computing foo4...
1957747796
Reusing computed foo4 result...
1957747796
Comment