עבור לתוכן

cpp - מחלקה, שחרור זכרון

Featured Replies

פורסם

האם יש צורך במחלקה הזו לשחרר איזשהו זכרון?


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 transferred
Product::Product(int id)
{
_id=id;
_next=NULL;
}

//Copy constructor
Product::Product(Product &p)
{
_id=p.getNumber();
_next=NULL;
}

//Destructor freeing allocated memory
Product::~Product()
{

}

//Returns 'Product' id number
int Product::getNumber()const
{
return _id;
}

//Sets current's Product '_next' to 'p'
void Product::setNext(Product &p)
{
_next=&p;
}

//Return addr of _next Product
Product *Product::getNext()const
{
return _next;
}

הרי לא הוקצה שום זכרון בבונה, או שאני צריך להקצות משהו?

תודה.

פורסם

אין לך מה לשחרר.

(למעשה אתה לא חייב לממש בכלל Deconctructor במקרה הזה)

חוץ מזה, טיפ קטן - ב-copy constructor, תעשה const Product &p.

זה יגרום לכך שתוכל להעביר לו קבוע.

פורסם
  • מחבר

אוקי תודה, שיניתי. עוד שאלה, למה זה בסדר


//Default constructor
Client::Client()
{
_name=new char[clientName]="NoName";
next=NULL;
}

וזה - לא


//Constructor with name
Client::Client(char *name)
{
strcpy(_name, name);
next=NULL;
}

פורסם
  • מחבר

אוקי, נראה לי מצאתי פתרון


//Default constructor
Client::Client()
{
_name=new char()="NoName";
next=NULL;
}

//Constructor with name
Client::Client(char *name)
{
strcpy(_name=new char(), name);
next=NULL;
}

השאלה אם לא תהיה עם זה בעיה?

פורסם

זה בכלל מתקמפל?

אולי ב- constructor הראשון הקומפיילר יכול להבין באיזה גודל ליצור את המערך, אבל ב- constructor השני אין לו שום דרך להבין את זה.

פורסם
  • מחבר

בגודל NAME לא? :s05:

וזה התקמפל. :hi:

פורסם

אני לא בטוח שהקוד שכתבת תקין... (למרות שהקומפילר מסתדר איתו)

הנה ההצעה שלי:


//Default constructor
Client::Client()
{
_name = new char[7];
strcpy(_name, "NoName");
next=NULL;
}

//Constructor with name
Client::Client(const char *name)
{
_name = new char[strlen(name)+1]
strcpy(_name, name);
next=NULL;
}

או לחילופין, לשלב את שני הקונסטרקטורים באחד:



//Constructor with name
Client::Client(const char *name = "NoName")
{
_name = new char[strlen(name)+1]
strcpy(_name, name);
next=NULL;
}

פורסם

אוקי, נראה לי מצאתי פתרון


//Default constructor
Client::Client()
{
_name=new char()="NoName";
next=NULL;
}

//Constructor with name
Client::Client(char *name)
{
strcpy(_name=new char(), name);
next=NULL;
}

השאלה אם לא תהיה עם זה בעיה?

מה שעשית כאן בעצם זה להקצות CHAR, ולהעתיק למקום שלו בזיכרון והלאה את המחרוזת שלך. אתה יכול לדרוס ככה דברים, ומערכת ההפעלה שלך לא כל כך אוהבת את זה.

פורסם
  • מחבר

אוקי, הבנתי איפה הטעות שלי. תודה

פורסם

לא לשכוח להשתמש ב-[]delete על מצביעים שהוקצו עם []new.

תודה בשם ההנהלה וצוות העובדים.

פורסם
  • מחבר

:jump:

תודה

עוד שאלה.

נגיד יש לי לדוגמה מחלקה

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;

פורסם

תלוי אם אתה רוצה ראש רשימה ריק או לא.

פורסם
  • מחבר

אם לא ריק אזי עם מצביע? :nixweiss:

פורסם

fi

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים