I have a class like this:
Expand|Select|Wrap|Line Numbers
- class MyClass
- {
- public:
- MyClass(int Count)
- {
- Data = new double[Count];
- };
- ~MyClass()
- {
- delete [] Data;
- }
- // Some methods to set/get the Data, modify the data,
- // a complete set of overloaded operators, etc.
- const MyClass operator + (const MyClass &Target) const
- {
- MyClass Temp(Count);
- Temp = *this;
- // The addition algorithm is here...
- return Temp;
- }
- private:
- double *Data;
- };
Expand|Select|Wrap|Line Numbers
- MyClass obj1(4), obj2(4), obj3(4);
- obj3 = obj1 + obj2;// Can't do this!
For a short time, I added a boolean class member called DeleteElements, and changed the destructor to this:
Expand|Select|Wrap|Line Numbers
- ~MyClass()
- {
- if (DeleteElements)
- delete [] Data;
- }
Expand|Select|Wrap|Line Numbers
- MyClass operator + (MyClass &Target) const
- {
- MyClass Temp(Count);
- Temp = *this;
- // The addition algorithm is here...
- Temp.DeleteElements = false;
- return Temp;
- }
Has anyone else run into these problems? Is there an elegant solution? I'd like to avoid completely re-writing the class, but I can if that's the most effective way to handle the problem.
Thanks for the help!
-Kerry