constructor and destructor [modified]
-
Hi, how can I write the code for the constructor and destructor of this class class ThreeD{ public: ThreeD(int=5, int=5, int=5); ~ ThreeD( ); private: int *** data; int l, w, d; }; thank you
modified on Thursday, March 25, 2010 10:50 PM
Actually, I think the design of your class "ThreeD" is terrible. If you are sure to implement it, the follow is for you. class ThreeD { public: ThreeD(int _l=5, int _w=5, int _d=5) : l(_l), w(_w), d(_d) { data = new int**[l]; for (int i=0; i<l; ++i) { data[i] = new int*[w]; for (int j=0; j<w; ++j) { data[i][j] = new int[d]; memset(data[i][j], 0, d*sizeof(int)); } } } ~ThreeD() { for (int i=0; i<l; ++i) { for (int j=0; j<w; ++j) { delete [] (data[i][j]); } delete [] (data[i]); } delete [] data; } private: int*** data; int l, w, d; };
-
Hi, how can I write the code for the constructor and destructor of this class class ThreeD{ public: ThreeD(int=5, int=5, int=5); ~ ThreeD( ); private: int *** data; int l, w, d; }; thank you
modified on Thursday, March 25, 2010 10:50 PM
zakria81 wrote:
Hi, how can I write the code for the constructor and destructor of this class
Your question is vague at best. Given what little you've provided, you can implement them any way you wish. Have a look at the "How to ask a question" thread on the first page of this forum.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius