Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Writing move constructors and move assignment

Reply
Thread Tools

Writing move constructors and move assignment

 
 
Andrew Tomazos
Guest
Posts: n/a
 
      12-12-2011
Suppose I have a C interface as follows:

// create a new FOO
FOO* foo_open(int params);

// use FOO one or more times
void foo_bar(FOO*);

// destroy FOO
foo_close(FOO*);

and I want to wrap that in a C++11 class.

class Foo
{
Foo(int params) : p(foo_open(params)) {}

void bar() { foo_bar(p); }

~Foo() { if (p) { foo_close(p); } }

private:
FOO* p;

// Clearly Foo cannot be copied otherwise you will call foo_close
twice for the same handle
Foo(const Foo&) = delete;
Foo& operator= (const Foo&) = delete;

// But we should be able to move it
Foo(Foo&& that)
{
???
}

Foo& operator= (Foo&&)
{
???
}
};

I am not clear how to implement the move constructor and move
assignment. ? What role does std::move play if any in this case?
-Andrew.

 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      12-12-2011
Andrew Tomazos wrote:

> Suppose I have a C interface as follows:
>
> // create a new FOO
> FOO* foo_open(int params);
>
> // use FOO one or more times
> void foo_bar(FOO*);
>
> // destroy FOO
> foo_close(FOO*);
>
> and I want to wrap that in a C++11 class.
>
> class Foo
> {
> Foo(int params) : p(foo_open(params)) {}
>
> void bar() { foo_bar(p); }
>
> ~Foo() { if (p) { foo_close(p); } }
>
> private:
> FOO* p;
>
> // Clearly Foo cannot be copied otherwise you will call foo_close
> twice for the same handle
> Foo(const Foo&) = delete;
> Foo& operator= (const Foo&) = delete;
>
> // But we should be able to move it
> Foo(Foo&& that)
> {
> ???
> }
>


Foo(Foo&& that)(that.p){that.p=0;}

> Foo& operator= (Foo&& that)
> {
> ???


std::swap(p,that.p);

OR

foo_close(p); p=that.p; that.p=0;
(it could be different if foo_close can throw)

> }
> };
>
> I am not clear how to implement the move constructor and move
> assignment. ?


Just try to consider what the destructor can do after you moved from
an object. Move assignment is often a synonym for swap (avoids
thinking too long) unless you want to be sure to destruct immediatly.

> What role does std::move play if any in this case?


Foo a(42);
Foo b=std::move(a);
a=std::move(b);

You may want to take a look at unique_ptr.
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      12-12-2011
On Mon, 12 Dec 2011 04:11:10 -0800, Andrew Tomazos wrote:

> Foo(Foo&& that)
> {
> ???

p = that.p;
that.p = NULL;
> }



>
> Foo& operator= (Foo&&)

Foo& operator= (Foo&& that)
> {
> ???

if (this != &that) {
p = that.p;
that.p = NULL;
}
return *this
> }
> };


> What role does std::move play if any in this case?


You can re-write the move constructor using the move assignment
operator and std::move as:

Foo(Foo&& that)
{
*this = std::move(that);
}

std::move just lets you get an rvalue reference for an lvalue, to cause
the correct overload to be used.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments