![]() |
|
#1
|
|||
|
|||
destruction functioni have a problem with the destruction function.it runs somewhere that i don't expect.I tried with a bunch number of compilers.same result.can u tell me the way it makes sense?
CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: destruction functionQuote:
Make the program tell you where it goes and when it goes there: CPP / C++ / C Code:
Output: Code:
Note that the stuff displayed at step after 4a may be garbage or not. The program may crash (or not) as it exits. You will find that the problem arises from the assignment statement s1 = "abcdef"; (That's where your unexpected destructor is called.) What happens is: 1. A constructor is called to make a temporary String object for the string literal. 2. The constructor allocates storage for a buffer and copies the string literal into the temporary object's str member. 3. The assignment is made: The value of s1.str is set equal to the address of the buffer containing the copy of the string literal. This is the operation of the default assignment operator for any class: a so-called "shallow copy". 4. The destructor is called for the temporary object (which deallocates the storage, and makes the value of s1.str invalid). When you have a class member that is a pointer and a constructor or other member function allocates storage and assigns its address to that pointer, then you have a problem with an assignment statement. If you don't create an assignment operator (overload the '=' operator), the default assignment operator simply copies the object. That is, the pointer value of the object on the left-hand side of the assignment is set equal to the value of the pointer on the right-hand side. (So it won't work if the object on the right hand side goes out of scope. And it won't work the way you need for it to work in any case.) The solution: make an overloaded assignment operator that actually allocates new storage and copies the contents rather than just using the default assignment operator. If you implement an overloaded assignment operator with debugging print statements along the lines that I showed above, you might see something like: Code:
The sequence from 2a through 2b shows what actually happens for an assignment like the one that you used in your program. Of course this is OK for illustration of the sequence of operations and for showing why you (usually) must overload the assignment operator in cases like this. For practical programs like this, you would use a std::string rather than an pointer to char for the class member. There are other times when you might actually need a pointer (to something) as a class member, and the things shown in this simple illustration will apply. Regards, Dave |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Message Class | TransformedBG | C++ Forum | 5 | 29-Nov-2006 22:28 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 12:33 |
| [GIM] gim.h | dsmith | C Programming Language | 0 | 18-Jan-2005 09:48 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 05:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The