G
Gernot Frisch
The following code crashes, since the delete[] in ~ds is called on a
wrong thingy... Can you help me out? Why does the temporary object get
destructed before the assignment occours???
Output of that code:
152433<bang>
#define MAX_STRLEN 512
class ds;
class cs // const string handler
{
public:
cs(const char* c) {printf("1");dat = c;}
operator ds() const;
const char* dat;
};
class ds // dynamic string handler
{
public:
ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat); return
*this;}
char* dat;
};
cs:perator ds()const {printf("5");ds a;a = *this;return a;}
int main(int argc, char* argv[])
{
ds a = cs("B");
}
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
wrong thingy... Can you help me out? Why does the temporary object get
destructed before the assignment occours???
Output of that code:
152433<bang>
#define MAX_STRLEN 512
class ds;
class cs // const string handler
{
public:
cs(const char* c) {printf("1");dat = c;}
operator ds() const;
const char* dat;
};
class ds // dynamic string handler
{
public:
ds(){printf("2");dat=new char[MAX_STRLEN]; dat[0]='\0';}
~ds() {printf("3");if(dat) delete[]dat; dat=NULL;}
ds& operator=(const cs& a) {printf("4");strcpy(dat, a.dat); return
*this;}
char* dat;
};
cs:perator ds()const {printf("5");ds a;a = *this;return a;}
int main(int argc, char* argv[])
{
ds a = cs("B");
}
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com