471,612 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 software developers and data experts.

Static member functions in Abstract Base Class

yan
Hi, everybody. I'm new to this great forum, so hello!
I start with a problem of my project.
I have an Abstract Base Class used as base class for differentes
Derived classes. The derived classes must able to set the behavior
af a function f() at run-time. I want use the same function behavior
also in Another Class, but without using inheritance because
Another Class not extend Base Class.

I have used a function pointer and static members function declared
in Base Class for differentes implementation of the function f().

Is correct? or is convenient to define a new class to share common
functionalities between Derived and Another class?
Here is the example code.

Expand|Select|Wrap|Line Numbers
  1. class AbstractBaseClass {
  2. public:
  3. virtual int f(int a, int b) = 0;
  4. virtual void setImplementation(int implType) = 0;
  5. static int fImplementationOne(int a, int b) {
  6. return a+b;
  7. }
  8. static int fImplementationTwo(int a, int b) {
  9. return a-b;
  10. }
  11. };
  12.  
  13. class DerivedClass : public AbstractBaseClass {
  14. public:
  15. DerivedClass(int implType) {
  16. setImplementation(implType);
  17. }
  18. void setImplementation(int implType) {
  19. if (implType == 0)
  20. _f = &AbstractBaseClass::fImplementationOne;
  21. else
  22. _f = &AbstractBaseClass::fImplementationTwo;
  23. }
  24. int f(int a, int b) {
  25. return _f(a, b);
  26. }
  27. private:
  28. int (*_f)(int a, int b);
  29. };
  30.  
  31. class AnotherClass {
  32. public:
  33. AnotherClass() {
  34. _f = &AbstractBaseClass::fImplementationOne;
  35. }
  36. private:
  37. int (*_f)(int a, int b);
  38. };
  39.  
  40. int main()
  41. {
  42. AnotherClass a;
  43. AbstractBaseClass *c = new DerivedClass(0);
  44. cout << c->f(10,3) << '\n';
  45. c->setImplementation(1);
  46. cout << c->f(10,3) << '\n';
  47. }
  48.  
  49.  
Jul 22 '05 #1
1 3020
"yan" <a_********@virgilio.it> wrote...
I start with a problem of my project.
I have an Abstract Base Class used as base class for differentes
Derived classes. The derived classes must able to set the behavior
af a function f() at run-time. I want use the same function behavior
also in Another Class, but without using inheritance because
Another Class not extend Base Class.

I have used a function pointer and static members function declared
in Base Class for differentes implementation of the function f().

Is correct? or is convenient to define a new class to share common
functionalities between Derived and Another class?
Here is the example code.
[...]


If the code is working, I don't see a C++ problem. What you're
trying to comprehend is the problem of design, not implementation.
In order to see whether your design is OK and to recommend any
changes to it, we need to know what problem you're trying to solve
by developing such design.

Also, consider posting to comp.object for more OOD and OOA advice.

V
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by Chris Capel | last post: by
7 posts views Thread by Sunny | last post: by
2 posts views Thread by Mark P | last post: by
8 posts views Thread by Per Bull Holmen | last post: by
14 posts views Thread by v4vijayakumar | last post: by
10 posts views Thread by Rahul | last post: by
2 posts views Thread by Markus Dehmann | last post: by
5 posts views Thread by Tim Frink | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.