changing virtual methods

Discussion in 'C++' started by dcipher, Aug 5, 2003.

  1. dcipher

    dcipher Guest

    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!
     
    dcipher, Aug 5, 2003
    #1
    1. Advertising

  2. > 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
     
    Kurt Krueckeberg, Aug 5, 2003
    #2
    1. Advertising

  3. dcipher

    tom_usenet Guest

    On Mon, 04 Aug 2003 19:27:23 -0400, dcipher <>
    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
     
    tom_usenet, Aug 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Simmons
    Replies:
    0
    Views:
    820
    Derek Simmons
    Aug 1, 2004
  2. Jéjé
    Replies:
    2
    Views:
    409
    Jéjé
    Nov 30, 2005
  3. while_1
    Replies:
    2
    Views:
    646
    Roedy Green
    Jun 25, 2004
  4. Jim Fischer
    Replies:
    3
    Views:
    455
    Jim Fischer
    Jul 31, 2003
  5. Kenneth McDonald
    Replies:
    5
    Views:
    355
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page