בעיה ב-return מסוג class.

akakor

New member
בעיה ב-return מסוג class.

יש לי class שעושה כל מיני מניפולציות על מחרוזות. באחת מהפונקציות שלה אני מקבל מספר integer ומחזיר את אותו מספר המילה מתוך המחרוזת. להלן הפונקציה:
SString strTemp(this->buf); if(k<NumOfWords() && k>=0) { strTemp.buf = strtok(strTemp.buf, delimit); for(int i=0; i<k; i++) strTemp.buf = strtok(NULL, delimit); return strTemp; } else { strTemp.buf = NULL; return strTemp; }​
כאשר אני מפעיל את ה-debugger אני רואה שכאשר הוא מגיע לשורה של return הכל טוב ויפה ורק ב-return הוא מעיף את התכנית. ב-main אני קורה לפונקציה בצורה הבאה:
s1.Concat(s2.Word(2));​
כאשר הפונקצייה concat עובדת בלי בעיות. כמו כן יש לי את כל הconstructors וגם destructor...
 

2expert

New member
answer

the problem is that when you exit the function the object is deleted therefore you should do one of two things 1. return a pointer to an heap defined object 2. create a copy constructor to the class
 

akakor

New member
יש לי...

יש לי copy constructor:
SString(const SString & s) {Assign(s.buf);}​
והוא עובד...
 
למעלה