473,387 Members | 1,398 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,387 software developers and data experts.

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::putPixel
// 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 1959
> 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::putPixel
// 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(lementation class)
PImage8 WindowsPlatformImplementationImage8
X11PlatformImplementationofImage8

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 WindowsPlatformImplementationImage8;
}
else if (/* platform is X11 */) {
imp_ = new WindowsPlatformImplementationImage8;
}
}
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****@neuraldk.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::putPixel
// 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 responsibilities, and then compose them together
into more fully featured classes, even classes with no virtual
methods. e.g.

class PImage
{
GraphicsSystem* m_graphicsSystem;
BitDepthDependentAlgorithms* m_bppAlgo;
CoordinateTransformation* 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
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...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
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...
175
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...
5
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
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...
4
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...
2
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...
318
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 ... ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.