Okay, here's why that deconstructor is being called. You are assigning a new value to your CString str2, which wasn't initialized--and since you didn't overload the operator=() for this type of thing, the default behavior is to call the deconstructor followed by the appropriate constructor. So, that's fine how it is, no real problem at all.
Also, on a side note, you seem to be using a lot of C in this C++ program (free, malloc, etc.). All of which is really more work than necessary. In addition, there is no such operator as 'operator char*' (your compiler might be fine with it, but it's not the ANSI/ISO standard) and you are casting to long in a depreciated manner ( (long) should be static_cast<long>(value_here) ). Finally, long isn't really a good type to return the size of a string, because a string's length can only be whole numbers greater than or equal to zero--so, the space for whole numbers less than zero will never be used. So, 'unsigned long' might be a little better, however, there is a type that is just for sizes--size_t--which is the best fit for what you want.
Most of the functionality that you are looking for is built right into the C++ STL string class, which would make your program a whole lot easier to write. Re-write example (using the string class):
string.hpp
Code:
#ifndef _STRING_HPP
#define _STRING_HPP
#include <string>
class CString
{
// Constructors
public:
CString(); // Default constructor
CString(const char * str); // Constructor taking a char* as parameter
CString(const string & str); // Constructor for a string
CString(const CString & str); // Constructor taking a reference to another CString object (not implemented)
~CString(); // Deconstructor
// Instance methods...
size_t GetLength() const; // Runs size() method
char* c_str() const; // When a char* is requested from the CString object
private:
string _itsString; // The string
};
#endif
string.cpp
Code:
#include "string.hpp"
#include <stdio.h>
/* Default constructor: NULLs _itsString */
CString::CString()
{
// Do nothing...
puts("CString::CString()");
}
/* Default deconstructor */
CString::~CString()
{
// No pointers, nothing to delete...
puts("CString::~CString()");
}
CString::CString(const char *str)
{
// Assign value...
_itsString = str;
puts("CString::CString(const char *str)");
printf(" str=%s\n", _itsString.c_str());
}
CString::CString(const string & str){
// Assign value...
_itsString = str;
}
/* Constructor taking a CString & (fully implemented) */
CString::CString(const CString & str)
{
// We use the this pointer to be sure
// it is assigned correctly; most compilers
// will not need this, but some older ones do.
this->_itsString = str.c_str();
puts("CString::CString(const CString *str)");
}
/* Returns the length of itsString with size() method */
size_t CString::GetLength() const
{
puts("CString::GetLength()");
// Return size using the class string's size() method...
return _itsString.size();
}
// Return a C style character string...
char* CString::c_str() const {
// const_cast throws away the const-ness;
// because you return a 'char *' and _itsString.c_str()
// returns a 'const char *' you need to get rid of the
// const-ness.
// c_str() is a method in class string that returns a C style
// string; a variable length array terminated by a nul character
// '\0'.
return const_cast<char *>(_itsString.c_str());
}
main.cpp
Code:
#include "string.hpp"
#include <stdio.h>
void main()
{
puts("* CString str1 = CString(\"Hello World\");");
CString str1 = CString("Hello World");
puts("* CString str2;");
CString str2;
puts("* CString str3 = \"this works\"");
CString str3 = "blah";
puts("* str2 = \"blah blah\"");
str2 = "This is the beginning of a beautiful CString object :)";
puts("* printf(str1.GetLength();");
printf("str1.GetLength() returns %d\n", str1.GetLength());
puts("* printf(str2.GetLength();");
printf("str2.GetLength() returns %d\n", str2.GetLength());
puts("* printf(str3.GetLength();");
printf("str3.GetLength() returns %d\n", str3.GetLength());
puts("* puts(str1.c_str());");
puts(str1.c_str());
puts("* puts(str2.c_str());");
puts(str2.c_str());
puts("* puts(str3.c_str());");
puts(str3.c_str());
}
Bookmarks