473,792 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing virtual methods

I'm in the process of rewritting my graphics library to be purely OO using
C++.

The following is a section of my heirarchy of classes:

PImage // defines all basic graphics routines as virtual functions
PImage8 // implements those routines for 8bpp
PImage15 // and for 15bpp
PImage16 // and 16bpp
PImage24 // and so on...
PImage32

What I would now like to do is define even more specific classes such as
X11Image, but instead of having to define X11Image8 ... X11Image32, I'm
wondering if it's possible to do something like the following:

class X11Image : public PImage {
X11Image() {
switch(bpp) {
case 8:
// set all virtual methods to those of PImage8, eg:
// putPixel = &PImage8::putPi xel
// this doesn't work, however... is there a way to do this?
break;
case 15:
// set all virtual methods to those of PImage15
break;
// and so on...!
}
}
}

Thanks a lot,
Jeff

PS: Any suggestions are greatly appreciated here!
Jul 19 '05 #1
2 1978
> I'm in the process of rewritting my graphics library to be purely OO using
C++.

The following is a section of my heirarchy of classes:

PImage // defines all basic graphics routines as virtual functions PImage8 // implements those routines for 8bpp
PImage15 // and for 15bpp
PImage16 // and 16bpp
PImage24 // and so on...
PImage32

What I would now like to do is define even more specific classes such as
X11Image, but instead of having to define X11Image8 ... X11Image32, I'm
wondering if it's possible to do something like the following:

class X11Image : public PImage {
X11Image() {
switch(bpp) {
case 8:
// set all virtual methods to those of PImage8, eg:
// putPixel = &PImage8::putPi xel
// this doesn't work, however... is there a way to do this?
break;
case 15:
// set all virtual methods to those of PImage15
break;
// and so on...!
}
}
}

Thanks a lot,
Jeff

PS: Any suggestions are greatly appreciated here!


I don't know much about graphics, but the Bridge pattern might help. The
Bridge pattern is: "used to decouple an abstraction--in your case, the
PImage abstraction--from its implementation so that the two can vary."

PImage would contain a pointer to the base of the implementation hierarchy
of platform-specific implementation classes, called PImageImp.

PImage-----imp----------->PImageImp(leme ntation class)
PImage8 WindowsPlatform ImplementationI mage8
X11PlatformImpl ementationofIma ge8

class PImageImp;
class PImage {
PImageImp *imp_;
public:
virtual void draw()=0;// abstract base class

// etc.
};

Each derived PImage class would forward it's methods to the
platform-specific implementation.
class PImage8 : public PImage {
public:
PImage8()
{
if( /*platform is Windows) {
imp_ = new WindowsPlatform ImplementationI mage8;
}
else if (/* platform is X11 */) {
imp_ = new WindowsPlatform ImplementationI mage8;
}
}
void draw()
{
imp_->draw(); // forward to platform-specific implementation
}
//. . .
};
The client code only uses PImage and its derived classes.

--kurt
Jul 19 '05 #2
On Mon, 04 Aug 2003 19:27:23 -0400, dcipher <jw****@neurald k.org>
wrote:
I'm in the process of rewritting my graphics library to be purely OO using
C++.

The following is a section of my heirarchy of classes:

PImage // defines all basic graphics routines as virtual functions
PImage8 // implements those routines for 8bpp
PImage15 // and for 15bpp
PImage16 // and 16bpp
PImage24 // and so on...
PImage32

What I would now like to do is define even more specific classes such as
X11Image, but instead of having to define X11Image8 ... X11Image32, I'm
wondering if it's possible to do something like the following:

class X11Image : public PImage {
X11Image() {
switch(bpp) {
case 8:
// set all virtual methods to those of PImage8, eg:
// putPixel = &PImage8::putPi xel
// this doesn't work, however... is there a way to do this?
break;
case 15:
// set all virtual methods to those of PImage15
break;
// and so on...!
}
}
}


Do your X11Image classes override any methods of PImage?

It sounds like you are trying to place two orthogonal things into the
same class heirarchy, which is a design error. Your X11 stuff is
presumably specific to X11 graphics, and this is orthogonal to how
many bits per pixel are in an image. So you need two separate
heirarchies, something like this:

PImage // defines all basic graphics routines as virtual
PImage8 // implements those routines for 8bpp
PImage15 // and for 15bpp
PImage16 // and 16bpp
PImage24 // and so on...
PImage32

Now, you want a separate heirarchy giving the operations you can
perform on an different heirarchy, say:

GraphicsSystem
X11
OpenGL

or whatever. Then your PImage implementations can contain a pointer to
a GraphicsSystem, and use this pointer to do the basic, bit depth
independent operations.

The precise breakdown depends on the the details of what PImage is,
but whatever you do, remember that composition is generally much more
powerful than inheritence. Break down the problem into abstract
classes with fewer responsibilitie s, and then compose them together
into more fully featured classes, even classes with no virtual
methods. e.g.

class PImage
{
GraphicsSystem* m_graphicsSyste m;
BitDepthDepende ntAlgorithms* m_bppAlgo;
CoordinateTrans formation* m_transform;
ImageStorage* m_storage;
int m_width;
int m_height;

public:
//implement methods using the composite parts
//no need for virtual functions, since
//behaviour can be changed by different
//subparts.
};

Tom
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
1788
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under Perforamce Tips and Tricks in .NET Applications, A .Net Developer Platform White Paper, at the very bottom, it says: The JIT cannot inline virtual methods, so you lose a potential optimization if you get rid of non-virtual methods.
32
4528
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
12149
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
175
8910
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
5
15854
by: ^MisterJingo^ | last post by:
Is it good pratice to make base class properties virtual? For example: public abstract class Person { protected string name; public virtual string Name { //get, set stuff here } }
3
4552
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
4
2346
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which template is created and I tried to define a virtual method, that's called from the base template ctor. Here's my example code: #include <iostream>
2
1485
by: mrclash | last post by:
Hello, I have a class that uses some variables to access a sql database, and I want to make a derived class that inherits all the methods of this class, because the derived one will do exactly the same process that the parent one, but with a postgres database, thus I've got to use diferent type variables to acces the database. There's one big function in the class that does all the DB stuff, and I want to know if there's any way to...
318
11140
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows sounds with C++?
0
9670
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
10211
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...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9033
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...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
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.