Answers to Exercises

1.The output of the compiler should look something like this:
Error E2247 STREX1.cpp 21: 'string::m_Length' is not accessible in function main()

Warning W8004 STREX1.cpp 28: 'Length' is assigned a value that is never used in function
 main()

This one is simple; since m_Length is a private member variable of string, a nonmember function such as main can't access it. Also, the compiler is warning us that we are never using the value of the Length variable.

2.The output of the compiler should look something like this:
Error E2247 STREX2.cpp 17: 'string::string()' is not accessible in function main()

This is also pretty simple. Since the default constructor string::string() is in the private area, it's impossible for a nonmember function such as main to use it. Notice that there was no error message about string::string(char* p); that constructor is in the public area, so main is permitted to create a string from a C string. It's just the default constructor that's inaccessible.

3.The output of the compiler should look something like this:
Error E2166 STREX3.cpp 16: Destructor for 'string' is not accessible in function main()

This answer is considerably less obvious than the previous ones. To be sure, the destructor is private and therefore can't be called from main, but that doesn't explain why main is trying to call the destructor in the first place. The reason is that every auto variable of a type that has a destructor must have its destructor called at the end of the function where the auto variable is defined. That's part of the mechanism that makes our objects act like "normal" variables, which also lose their values at the end of the function where they are declared.[25] In the case of a user defined variable, though, more cleanup may be required; this is certainly true for strings, which have to deallocate the memory that they allocated to store their character data.

[25] To be more precise, the destructor is called at the end of the scope in which the variable was defined. It's possible for a variable to have scope smaller than an entire function; in that case, the variable is destroyed when its scope expires.

Therefore, you cannot create an object of a class whose destructor is private as an auto variable, as the automatic call of the destructor at the end of the scope would be illegal.

Susan didn't get this one exactly right, but she was close.

Susan: I have a note here that this program would not work because the ~string () thingy should be public and that, if this were to run, it would cause a memory leak. Am I on the right track?

Steve: Yes, you're close. Actually, what would happen is that the compiler would refuse to compile this program because it wouldn't be able to call ~string at the end of the function, since ~string is private. If the compiler would compile this program without calling the destructor, there would indeed be a memory leak.

4.Let's take a look at the sequence of events that would have transpired if the user had typed a = a;.[26] The first statement to be executed would be char* temp = new char[Str.m_Length], which would allocate some new memory to store the contents of the string.

[26] See Figure 7.15 on page 446 for the code for operator =.

The second statement to be executed would be m_Length = Str.m_Length;. Since m_Length and Str.m_Length are actually the same memory location in this case, this statement wouldn't do anything.

Then the statement memcpy(temp,Str.m_Data,m_Length); would be executed. This would copy m_Length bytes of data to the address stored in temp, which points to the newly allocated piece of memory, from the address stored in Str.m_Data.

The next statement to be executed would be delete [ ] m_Data;, which would free the memory previously allocated to our string.

The next statement to be executed would be m_Data = temp;. This would cause our string to refer to the newly allocated memory, which contains a copy of the data from the right hand string (in this case, the same string we are assigning to).

Finally, we would return *this, as we normally do from an assignment operator.

The net result of all of this is that the m_Data member variable of string a would point to a copy of the same data that it originally pointed to; in other words, the assignment statement would work correctly even for this case.

Susan had a very concise summary of this process:

Susan: So the data for the original string was stolen and replaced with an exact replica?

Steve: Wright.[27]

[27] See http://www.wam.umd.edu/~stwright/right/StevenWright.html for more Steve Wright jokes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset