jeudi 6 décembre 2018

Can I use typedef for an array length?

I have a class that I want to use to represent a generalized point on a bezier curve. Right now it looks like this:

//BezierCurvePoint.h

class BezierCurvePoint
{
    int _pointDepth;
    double* _pointData;
    int _layerDepth;
    double* _layerData;

public:
    BezierCurvePoint(int pointDepth, int layerDepth = 0);

    //Lots of operator methods to make math easier
}


//BezierCurvePoint.cpp

BezierCurvePoint::BezierCurvePoint(int pointDepth, int layerDepth) :
    _pointDepth(pointDepth),
    _layerDepth(layerDepth)
{
    _pointData = pointDepth ? new double[pointDepth] : nullptr;
    _layerData = layerDepth ? new double[layerDepth] : nullptr;
}

The _pointData represents the coordinates for the point and the _layerData is some extra information I want to store per point such as line width and tilt.

Unfortunately, I'm also creating and deleting lots of tiny arrays. Since all my points are going to have the same pointDepth and layerDepth, it would be useful if I could figure out a typedef (or some other refinement) of this more general case so that I'm using fixed length arrays instead of allocating them. It would also make debugging easier.

I don't want to use templates since I have some lengthy functions that are defined in the .cpp which I'd prefer to keep out of the header.

Is there a way to do this?

Aucun commentaire:

Enregistrer un commentaire