CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2007
    Location
    Scotland
    Posts
    137

    refactoring a class...

    hi there, I'm trying to think of a better solution to this:

    I have a polygon class that has a lot of operations for clipping etc. It mainly performs operations on a point struct (x, y), but sometimes I need to couple data with this point struct and have it perform a different operation.

    take for example a clipping operation... the polygon class holds an array of points (x, y) and they have to be clipped to a line, this function would output another polygon that has been clipped to the line, but I have another structure that holds a point(x, y) and a color(int). When I perform the clipping operation I wan't (along with the new positions) a new color value for any points that have been clipped (interpolated between the previous and new position).

    The way I'm currently doing this is by defining my polygon class as such:

    Code:
    template <class Real,  template <class> class Type>
    class CPolygon3D
    {
    private:
    
       Type<Real> Points;
    };

    the Real is what data type the x and y are in (e.g float, int etc.) and the Type has to be a container with certain callback functions.

    Code:
    template <class Real>
    class CStdPoint
    {
       CPoint<Real> Pos;
    
       CStdPoint intersect(CLine line);
    };
    
    
    template <class Real>
    class CColorPoint
    {
       CPoint<Real> Pos;
       int Color;
    
       CColorPoint intersect(CLine line);
    };

    the Pos member and intersect function have to be in the class if you want to use it within the polygon structure, in the CColorPoint class the intersect function will calculate the new color value and new position wheras in the CStdPoint it will just calculate the new position.

    Is this a good way to solve this? or can anyone think of a better way? It just feels a bit messy.

  2. #2
    Join Date
    Feb 2009
    Posts
    8

    Re: refactoring a class...

    Code:
    template<class FUNC>
    class Helper
    {
       FUNC intersect(CLine line);
    }
    
    template <class Real>
    class CStdPoint
    {
       CPoint<Real> Pos;
    };
    
    
    template <class Real>
    class CColorPoint
    {
       CPoint<Real> Pos;
       int Color;
    };
    ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured