473,231 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 3099
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
2
by: Mark P | last post by:
Still being relatively new to C++ I like to go back to review and rethink old designs to see where I could do things better. The issue I'm currently pondering is static vs. dynamic polymorphism,...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
2
by: Markus Dehmann | last post by:
I need a simple object serialization, where loading an object from file looks like this: Foo* foo1 = FooFactory::create("./saved/foo1.a321f23d"); Foo* foo2 =...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.