Re: C++ std::string tries to free() a not allocated pointer ?
Re: C++ std::string tries to free() a not allocated pointer ?
- Subject: Re: C++ std::string tries to free() a not allocated pointer ?
- From: Howard Moon <email@hidden>
- Date: Thu, 19 Nov 2009 07:59:08 -0800
On Nov 19, 2009, at 7:48 AM, David Rowland wrote:
On Nov 19, 2009, at 7:41 AM, Howard Moon wrote:
On Nov 19, 2009, at 7:26 AM, Paul Sanders wrote:
What does your destructor do? Your copy constructors / assignment
operators
appear to copy the pointer, rather than making a copy of the data
pointed
to, so perhaps you end up freeing the pointer twice (once for each
SQLString
object destroyed).
That would be true if realStr were a char*. But it's defined as a
std::string, so its assignment operator is used, which would
perform the string copy properly.
A rule of thumb with C++ is that the assignment operator, copy
constructor and destructor tend to come as a package. If you need
one, you often need all three.
I agree, but you missed my point. I was disagreeing with the
contention that the copy constructors / assignment operators simply
copied the passed pointer. They do not. The assignment here:
const SQLString & operator=(const char * s)
{
realStr = s;
return *this;
}
... calls the std::string assignment operator to copy the string's
contents to itself, because realStr is a std::string, not a char*.
-Howard
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden