פורסם 2006 במאי 1019 שנים האם יש צורך במחלקה הזו לשחרר איזשהו זכרון?class Product{public: Product(); Product(int id); Product(Product &p); ~Product(); int getNumber()const; void setNext(Product &p); Product *getNext()const;private: int _id; //ID number of kit Product *_next; //Pointer to next 'Product'};Product::Product(){ _id=-1; _next=NULL;}//Constructor copying new 'Product' from this that transferredProduct::Product(int id){ _id=id; _next=NULL;}//Copy constructorProduct::Product(Product &p){ _id=p.getNumber(); _next=NULL;}//Destructor freeing allocated memoryProduct::~Product(){}//Returns 'Product' id numberint Product::getNumber()const{ return _id;}//Sets current's Product '_next' to 'p'void Product::setNext(Product &p){ _next=&p;}//Return addr of _next ProductProduct *Product::getNext()const{ return _next;}הרי לא הוקצה שום זכרון בבונה, או שאני צריך להקצות משהו?תודה.
פורסם 2006 במאי 1019 שנים אין לך מה לשחרר.(למעשה אתה לא חייב לממש בכלל Deconctructor במקרה הזה)חוץ מזה, טיפ קטן - ב-copy constructor, תעשה const Product &p.זה יגרום לכך שתוכל להעביר לו קבוע.
פורסם 2006 במאי 1019 שנים מחבר אוקי תודה, שיניתי. עוד שאלה, למה זה בסדר//Default constructorClient::Client(){ _name=new char[clientName]="NoName"; next=NULL;}וזה - לא //Constructor with nameClient::Client(char *name){ strcpy(_name, name); next=NULL;}
פורסם 2006 במאי 1019 שנים מחבר אוקי, נראה לי מצאתי פתרון//Default constructorClient::Client(){ _name=new char()="NoName"; next=NULL;}//Constructor with nameClient::Client(char *name){ strcpy(_name=new char(), name); next=NULL;}השאלה אם לא תהיה עם זה בעיה?
פורסם 2006 במאי 1119 שנים זה בכלל מתקמפל?אולי ב- constructor הראשון הקומפיילר יכול להבין באיזה גודל ליצור את המערך, אבל ב- constructor השני אין לו שום דרך להבין את זה.
פורסם 2006 במאי 1119 שנים אני לא בטוח שהקוד שכתבת תקין... (למרות שהקומפילר מסתדר איתו)הנה ההצעה שלי://Default constructorClient::Client(){_name = new char[7];strcpy(_name, "NoName"); next=NULL;}//Constructor with nameClient::Client(const char *name){_name = new char[strlen(name)+1]strcpy(_name, name); next=NULL;}או לחילופין, לשלב את שני הקונסטרקטורים באחד://Constructor with nameClient::Client(const char *name = "NoName"){_name = new char[strlen(name)+1]strcpy(_name, name); next=NULL;}
פורסם 2006 במאי 1119 שנים אוקי, נראה לי מצאתי פתרון//Default constructorClient::Client(){ _name=new char()="NoName"; next=NULL;}//Constructor with nameClient::Client(char *name){ strcpy(_name=new char(), name); next=NULL;}השאלה אם לא תהיה עם זה בעיה?מה שעשית כאן בעצם זה להקצות CHAR, ולהעתיק למקום שלו בזיכרון והלאה את המחרוזת שלך. אתה יכול לדרוס ככה דברים, ומערכת ההפעלה שלך לא כל כך אוהבת את זה.
פורסם 2006 במאי 1119 שנים לא לשכוח להשתמש ב-[]delete על מצביעים שהוקצו עם []new.תודה בשם ההנהלה וצוות העובדים.
פורסם 2006 במאי 1119 שנים מחבר תודה עוד שאלה. נגיד יש לי לדוגמה מחלקה class Product{public: Product(); Product(int id); Product(Product &p); ~Product(); int getNumber()const; void setNext(Product &p); Product *getNext()const;private: int _id; //ID number of kit Product *_next; //Pointer to next 'Product'}; האם במחלקה Client שדה Product אמור להיות מצביע או לא? איך יהיה יותר לנכון לבצעה זאת. Product _kit; או Product *_kit;
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.