问题
I have a class where in a specific case some of its attributes need to reference an external variable. I managed to do in this way, but is there a better way?
#include "Vector.h"
class LineSeg
{
private:
Vector* a;
Vector* b;
bool owns;
public:
LineSeg(){
a = new Vector();
b = new Vector();
owns = true;
}
LineSeg(Vector ap, Vector bp){
a = new Vector(ap);
b = new Vector(bp);
owns = true;
}
LineSeg(Vector &ap, Vector &bp, bool owns){
a = ≈
b = &bp;
owns = false;
}
virtual ~LineSeg(){
if(owns){
delete a;
delete b;
}
}
};
I can't make two different classes.
回答1:
When doing such things (happens occasionally especially for mutexes), I usually use pointer to members. This avoids any special treatment and delete in the destructor.
#include "Vector.h"
class LineSeg
{
private:
Vector a0;
Vector b0;
Vector* a = &a0;
Vector* b = &b0;
public:
LineSeg()
{
}
LineSeg(Vector &ap, Vector &bp, bool copy) {
if( copy )
{
a0 = ap;
b0 = bp;
return;
}
a = ≈
b = &bp;
}
virtual ~LineSeg(){
}
};
However, this comes with some memory cost when external references are used, since a0 and b0 still use space in the class, though not used. However, such a construct is necessary rarely, and it's worth the little memory cost imo.
But design-wise this construct is questionnable, because it is potentially confusing and dangerous. Let's say you add a method clear(), which clears the both Vector members:
void clear() {
a->clear();
b->clear();
}
Now a colleague creates a LineSeg( myVector1, myVector2, false) and after some computing calls myLineSeg.clear() - that might be pretty confusing, because his own myVector1 and myVector2 are cleared.
That's why I limit this to special cases like mutexes, and usually only allow it protected.
Im most other cases vu1p3n0x is right - it's much better to use only one mechanism - either copy always, take ownership always, or use shared_ptr.
来源:https://stackoverflow.com/questions/48275172/class-with-attributes-referencing-an-external-variable-only-in-some-cases