473,670 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3118
"yan" <a_********@vir gilio.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
3333
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
7
1875
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 constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
2
1745
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, or equivalently in my case, templates vs abstract base classes. Let me be concrete. I have a class for performing a geometry operation which takes a collection of line segments and breaks them into minimal nonintersecting subsegments. I wrote...
8
8924
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. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
14
4195
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
2
2406
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 static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
10
2085
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 the reason behind this restriction... i just want to want a base class to be abstract so as to avoid object slicing into the base type from derived type, and in my case base class doesn't need to have a virtual function.
2
2304
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 = FooFactory::create("./saved/foo2.eb287ac8"); Now, Foo is an abstract base class, and FooFactory contains a static function which again calls static create functions on Foo1 or Foo2 (see code for all classes below). The problem is that all these functions are static, but I also...
5
4652
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
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8813
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.