D
David Williams
Hi Guys, I have a couple of problems. The first is (I believe) a simple
syntactic problem you can probably solve quite easily. The second is a
design style problem which might be more tricky...
So, I have a class with a couple of integers. I provide a couple of
functions to operate on those integers (add, subtract) and the function
to be called is chosen via a function pointer. Like so:
01: #include <iostream>
02:
03: class MyClass
04: {
05: public:
06: MyClass(int xToSet, int yToSet):x(xToSet), y(yToSet){};
07:
08: void setOperationToAdd(void)
09: {
10: performOperation = add;
11: }
12:
13: void setOperationToSubtract(void)
14: {
15: performOperation = subtract;
16: }
17:
18: //Pointer to an operation.
19: int (MyClass::*performOperation)(void);
20:
21: protected:
22: int add(void){return x + y;};
23: int subtract(void){return x - y;};
24: int x;
25: int y;
26: };
27:
28: int main()
29: {
30: MyClass test(10,20);
31: test.setOperationToAdd();
32: std::cout << test.performOperation() << std::endl;
33: test.setOperationToSubtract();
34: std::cout << test.performOperation() << std::endl;
35:
36: return 0;
37: }
Visual Studio gives the error:
error C2064: term does not evaluate to a function taking 0 arguments
for lines 32 and 34. Any idea what the problem is?
Ok, now for the design problem. I wish to allow the user of my class to
specify their own operations (e.g. multiply, divide). The problem I have
is that these new functions are declared outside the class, they are not
member functions (because presumably the user wouldn't be able to modify
my class). Now, pointers to member functions (such as the one i've
declared) cannot point to normal functions because of the implicit
'this' parameter. How should I solve this?
1) I could require my users to subclass 'MyClass' if they want to add
new functions. Their new functions would then be members and so could be
pointed to. This has the advantage that they can directly access the
protected parts without accessor functions but seems a bit of a burden
on the user.
2) I read (briefly) about funtionoids in the FAQ's. It seens they can be
used when functions have varying parameters as common ones get passed to
the constructor of the functionoid and un-common ones get passed when
the functionoid is called. Can these be used in this situation?
Any furthers ideas?
Thanks in advance,
David
syntactic problem you can probably solve quite easily. The second is a
design style problem which might be more tricky...
So, I have a class with a couple of integers. I provide a couple of
functions to operate on those integers (add, subtract) and the function
to be called is chosen via a function pointer. Like so:
01: #include <iostream>
02:
03: class MyClass
04: {
05: public:
06: MyClass(int xToSet, int yToSet):x(xToSet), y(yToSet){};
07:
08: void setOperationToAdd(void)
09: {
10: performOperation = add;
11: }
12:
13: void setOperationToSubtract(void)
14: {
15: performOperation = subtract;
16: }
17:
18: //Pointer to an operation.
19: int (MyClass::*performOperation)(void);
20:
21: protected:
22: int add(void){return x + y;};
23: int subtract(void){return x - y;};
24: int x;
25: int y;
26: };
27:
28: int main()
29: {
30: MyClass test(10,20);
31: test.setOperationToAdd();
32: std::cout << test.performOperation() << std::endl;
33: test.setOperationToSubtract();
34: std::cout << test.performOperation() << std::endl;
35:
36: return 0;
37: }
Visual Studio gives the error:
error C2064: term does not evaluate to a function taking 0 arguments
for lines 32 and 34. Any idea what the problem is?
Ok, now for the design problem. I wish to allow the user of my class to
specify their own operations (e.g. multiply, divide). The problem I have
is that these new functions are declared outside the class, they are not
member functions (because presumably the user wouldn't be able to modify
my class). Now, pointers to member functions (such as the one i've
declared) cannot point to normal functions because of the implicit
'this' parameter. How should I solve this?
1) I could require my users to subclass 'MyClass' if they want to add
new functions. Their new functions would then be members and so could be
pointed to. This has the advantage that they can directly access the
protected parts without accessor functions but seems a bit of a burden
on the user.
2) I read (briefly) about funtionoids in the FAQ's. It seens they can be
used when functions have varying parameters as common ones get passed to
the constructor of the functionoid and un-common ones get passed when
the functionoid is called. Can these be used in this situation?
Any furthers ideas?
Thanks in advance,
David