Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Moving objects in C++

Name: Cudder !MhMRSATORI 2015-02-03 11:43

C++ has new (constructor), delete (destructor), but what happened to reallocating and moving, something that is so easily done in C?

What gets in the way is that there may be pointers to the moved object, and this is the most common argument against it. In C++11 "move semantics" were introduced which is far more complex and convoluted than the bleedingly obvious solution of... update the pointers to the moved object!

In other words, they could just add a special member function that is called when an object is moved, with one parameter - its new address.

class Bar;
class Foo {
Bar *b;
Foo() { b = 0; }
Foo(Bar *b) {
this->b = b;
}
void move(void *x) {
b->f = x;
}
};

class Bar {
Foo *f;
Bar() { f = 0; }
Bar(Foo *f) {
this->f = f;
}
void move(void *x) {
f->b = x;
}
};


If a Foo is moved, then its Bar needs to be updated, and vice-versa. Of course this doesn't work in all cases because not all objects can know what points to them, but for those that do, the solution is trivial and it makes realloc work. To not provide a language feature for moving any object, just because some of them can't be moved, is absurd.

Name: Anonymous 2015-02-04 11:27

>>1
Oh god, you're literally retarded.

The whole and only purpose of move semantics is to avoid moving memory. Like, actually copying it over. Because copying memory over is slow. And for copying memory we've already had copy ctors.

Daaaaamn son, your post is the stupidest thing I've read in the past three days, if not the whole week.

How to impress friends and acquaintances with unbearable idiocy in three easy steps: 1) find a subject matter you know nothing about, 2) invent an elaborate theory of what it is about based on its name, 3) post a solution that solves that imaginary thing better.

IHBT

Name: Anonymous 2015-02-04 11:32

>>1
I just can't, I lost my ability to can.
In other words, they could just add a special member function that is called when an object is moved, with one parameter - its new address.
C(const C& other) {
printf("My new address is %p", this);
}

http://ideone.com/3z4Bji

How do you even? Because now I can't even.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List