Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can it delete 'ptr' in this move function?

26 views
Skip to first unread message

fl

unread,
Aug 7, 2016, 9:21:16 AM8/7/16
to
Hi,

When I read the following snippet on move function in a class, I am curious
about:

delete ptr;

in the move assignment. After this deletion, it assigns a new pointer value:

ptr = x.ptr;

What is deleted? I see a pointer is deleted, then it assigns a new pointer
value. Could you explain it to me?

Thanks,


class Example6 {
string* ptr;
public:
Example6 (const string& str) : ptr(new string(str)) {}
~Example6 () {delete ptr;}
// move constructor
Example6 (Example6&& x) : ptr(x.ptr) {x.ptr=nullptr;}
// move assignment
Example6& operator= (Example6&& x) {
delete ptr;
ptr = x.ptr;
x.ptr=nullptr;
return *this;
}
// access content:
const string& content() const {return *ptr;}
// addition:
Example6 operator+(const Example6& rhs) {
return Example6(content()+rhs.content());
}
};

Paavo Helde

unread,
Aug 7, 2016, 9:53:54 AM8/7/16
to
On 7.08.2016 16:18, fl wrote:
> Hi,
>
> When I read the following snippet on move function in a class, I am curious
> about:
>
> delete ptr;
>
> in the move assignment. After this deletion, it assigns a new pointer value:
>
> ptr = x.ptr;
>
> What is deleted? I see a pointer is deleted, then it assigns a new pointer
> value. Could you explain it to me?


ptr is a pointer to a string. The delete statement deletes not a
pointer, but the thing pointed to, namely the previous string what the
object was holding. Otherwise the previous string would be leaked. The
same is done in the destructor, for example.

HTH
Paavo

Öö Tiib

unread,
Aug 7, 2016, 10:20:06 AM8/7/16
to
On Sunday, 7 August 2016 16:21:16 UTC+3, fl wrote:
> Hi,
>
> When I read the following snippet on move function in a class, I am curious
> about:
>
> delete ptr;

What book you use for learning C++?
Delete expression destructs object(s) previously allocated by the new
expression and releases obtained memory area. The object destructed and
memory area released are pointed at by that 'ptr'.

>
> in the move assignment. After this deletion, it assigns a new pointer value:
>
> ptr = x.ptr;

Yes since the value of 'ptr' did become invalid (pointing at destructed and
released memory).

>
> What is deleted? I see a pointer is deleted, then it assigns a new pointer
> value. Could you explain it to me?
>

If 'ptr' is raw pointer to 'std::string' like it seems to be in code below
then it is awful example. The 'std::string' itself is made to get rid of
ad nauseum management of memory pointed at by raw pointer to 'char' and
already manages everything perfectly what is needed. Doing 'new string'
is inefficient and error-prone manual over-management and bloat of code.

>
> class Example6 {
> string* ptr;
> public:
> Example6 (const string& str) : ptr(new string(str)) {}
> ~Example6 () {delete ptr;}
> // move constructor
> Example6 (Example6&& x) : ptr(x.ptr) {x.ptr=nullptr;}
> // move assignment
> Example6& operator= (Example6&& x) {
> delete ptr;
> ptr = x.ptr;
> x.ptr=nullptr;
> return *this;
> }
> // access content:
> const string& content() const {return *ptr;}
> // addition:
> Example6 operator+(const Example6& rhs) {
> return Example6(content()+rhs.content());
> }
> };

Lot shorter cleaner and more efficient that does same:

class RealExample6
{
std::string str_;
public:
explicit RealExample6(std::string const& str)
: str_(str)
{}

// access content:
std::string const& content() const {return str_;}

// make 'operator+=' as public member and 'operator+' as nonmember
RealExample6& operator+=(RealExample6 const& that)
{
str_ += that.str_;
return *this;
}
};

RealExample6 operator+(RealExample6 lhs, Realexample6 const& rhs)
{
return lhs += rhs;
}

fl

unread,
Aug 7, 2016, 10:40:57 AM8/7/16
to
Thanks for your replies.

I've been learning C++ and using it occasionally. The definitions are not
firmly remembered. I see that there is a namespace line in the original code,
see below please. In this way, it addresses your concern or not?




#include <iostream>
#include <string>
using namespace std;

class Example6 {
string* ptr;
public:
Example6 (const string& str) : ptr(new string(str)) {}
~Example6 () {delete ptr;}
// move constructor
Example6 (Example6&& x) : ptr(x.ptr) {x.ptr=nullptr;}
// move assignment
Example6& operator= (Example6&& x) {
delete ptr;
ptr = x.ptr;
x.ptr=nullptr;
return *this;
}
// access content:
const string& content() const {return *ptr;}
// addition:
Example6 operator+(const Example6& rhs) {
return Example6(content()+rhs.content());
}
};



Thanks.
0 new messages