P
PengYu.UT
Hi,
Suppose I have the following class declaration with a pointer as the
argument. And suppose I "new" an array "lengths" as the argument
"edge_lengths". Now, the polygon object is valid. Unfortunately, it
becomes invalid after I delete "lengths" array be mistake.
My question is that using object, the object's(an polygon object in
this case) internal states are encapsulated. They should not be affect
by anything operations unless the member operator. But the above
example shows that the object's internal states can be affected if
pointers are used. I'm wondering if there are any solutions to fix it.
class polygon{
public:
polygon(int edge_lengths[], int edge_count) :
_edge_lengths(edge_lengths),_edge_count(edge_count) {}
}
private:
int *_edge_lengths;
int _edge_count;
};
Best wishes,
Peng
Suppose I have the following class declaration with a pointer as the
argument. And suppose I "new" an array "lengths" as the argument
"edge_lengths". Now, the polygon object is valid. Unfortunately, it
becomes invalid after I delete "lengths" array be mistake.
My question is that using object, the object's(an polygon object in
this case) internal states are encapsulated. They should not be affect
by anything operations unless the member operator. But the above
example shows that the object's internal states can be affected if
pointers are used. I'm wondering if there are any solutions to fix it.
class polygon{
public:
polygon(int edge_lengths[], int edge_count) :
_edge_lengths(edge_lengths),_edge_count(edge_count) {}
}
private:
int *_edge_lengths;
int _edge_count;
};
Best wishes,
Peng