I am implementing a C ++ math library. The library will be compiled into a DLL, so those who use it will only need header files, class definitions.
Users of my classes will be people who are not familiar with the language. However, there are some objects that can be referenced in several parts of their programs. Since I do not expect them to be involved in memory management, I would like to do it myself. So I need to do reference counting (garbage collection is not an option).
I want to make this reference count as transparent as possible, for example ...
CVecList pts;
pts.Add(Vector(0,0,0));
pts.Add(Vector(0,0,100));
pts.Add(Vector(0,100,0));
pts.Add(Vector(0,100,100));
CCurve* c1 = new CBezier(pts);
pts.Clear();
pts.Add(Vector(0,0,0));
pts.Add(Vector(0,200,100));
pts.Add(Vector(0,200,200));
pts.Add(Vector(0,-200,100));
pts.Add(Vector(0,-200,200));
pts.Add(Vector(0,0,0));
CCurve* c2 = new CBSpline(pts,3);
c1 = c2;
Things get a little more complicated when I define surface objects, because some surfaces are defined in terms of two curves:
CVecList pts;
CCurve* f = new CBezier(pts);
pts.Clear();
CCurve* g = new CBezier(pts);
CSurface* s = new CMixed(f,g);
, operator = :
typedef CReferenceCounted* PRC;
PRC& operator =(PRC& dest, PRC& source)
{
if (source)
source->AddRef();
if (dest)
dest->Release();
memcpy(&dest,&source,sizeof(PRC));
return dest;
}
... , operator = , .
- ?