Why is move constructor used over copy constructor?
This is simple program that is supposed to handle a dynamic array of numbers and then to filter out the elements that are even and put them into a new array and then print both arrays on the screen.
This is the header file:
#pragma once
namespace filter
class Array
double *arr;
int n;
public:
Array();
Array(int);
Array(const Array&);
Array(Array&&);
void PutIn();
void PrintOut() const;
Array isEven();
Array filter(const std::function<bool(int)>&) const;
~Array();
;
Then, this is the implementation of functions:
#include <iostream>
#include <functional>
#include "Array.h";
using namespace filter;
using namespace std;
Array::Array() :arr(nullptr), n(0)
Array::Array(int n)
this->n = n;
arr = new double[n];
Array::Array(const Array &a1)
n = a1.n;
arr = new double[n];
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
Array::Array(Array &&a1)
n = a1.n;
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
a1.n = 0;
a1.arr = nullptr;
void Array::PutIn()
cout << "Insert elements:n";
for (int i = 0; i < n; i++)
cin >> arr[i];
void Array::PrintOut() const
cout << "nYour array is :n";
for (int i = 0; i < n; i++)
cout << arr[i] << "t";
Array Array::isEven()
return filter((int x) return x % 2; );
Array Array::filter(const std::function<bool(int)> &f) const
int b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
b++;
Array temp(b);
b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
temp.arr[b] = arr[i];
b++;
return temp;
Array::~Array()
deletearr;
n = 0;
Finally, this is the source code:
#include <iostream>
#include <functional>
#include "Array.h"
using namespace filter;
using namespace std;
int main()
Array a(5);
a.PutIn();
Array b = a.isEven(); //WHY THIS LINE OF CODE INVOKES MOVE CONSTRUCTOR AND NOT COPY CONSTRUCTOR?
a.PrintOut();
b.PrintOut();
getchar();
getchar();
So, as you can see, this is relatively simple program that needs to handle an array with five elements in it entered by user and then to create a new array that consists of even elements of the first array. When i run this, it works fine, however, there is one little thing that i don't understand here.
If you look at the source code, notice the line where i left my comment, that is the line where move constructor is called, but i don't know why. That would mean that a.IsEven() is a RVALUE, since move constructor works with RVALUES, right? Can anyone explain me why this is rvalue and what is the correct way to understand this? Any help appreciated!
c++ oop
|
show 7 more comments
This is simple program that is supposed to handle a dynamic array of numbers and then to filter out the elements that are even and put them into a new array and then print both arrays on the screen.
This is the header file:
#pragma once
namespace filter
class Array
double *arr;
int n;
public:
Array();
Array(int);
Array(const Array&);
Array(Array&&);
void PutIn();
void PrintOut() const;
Array isEven();
Array filter(const std::function<bool(int)>&) const;
~Array();
;
Then, this is the implementation of functions:
#include <iostream>
#include <functional>
#include "Array.h";
using namespace filter;
using namespace std;
Array::Array() :arr(nullptr), n(0)
Array::Array(int n)
this->n = n;
arr = new double[n];
Array::Array(const Array &a1)
n = a1.n;
arr = new double[n];
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
Array::Array(Array &&a1)
n = a1.n;
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
a1.n = 0;
a1.arr = nullptr;
void Array::PutIn()
cout << "Insert elements:n";
for (int i = 0; i < n; i++)
cin >> arr[i];
void Array::PrintOut() const
cout << "nYour array is :n";
for (int i = 0; i < n; i++)
cout << arr[i] << "t";
Array Array::isEven()
return filter((int x) return x % 2; );
Array Array::filter(const std::function<bool(int)> &f) const
int b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
b++;
Array temp(b);
b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
temp.arr[b] = arr[i];
b++;
return temp;
Array::~Array()
deletearr;
n = 0;
Finally, this is the source code:
#include <iostream>
#include <functional>
#include "Array.h"
using namespace filter;
using namespace std;
int main()
Array a(5);
a.PutIn();
Array b = a.isEven(); //WHY THIS LINE OF CODE INVOKES MOVE CONSTRUCTOR AND NOT COPY CONSTRUCTOR?
a.PrintOut();
b.PrintOut();
getchar();
getchar();
So, as you can see, this is relatively simple program that needs to handle an array with five elements in it entered by user and then to create a new array that consists of even elements of the first array. When i run this, it works fine, however, there is one little thing that i don't understand here.
If you look at the source code, notice the line where i left my comment, that is the line where move constructor is called, but i don't know why. That would mean that a.IsEven() is a RVALUE, since move constructor works with RVALUES, right? Can anyone explain me why this is rvalue and what is the correct way to understand this? Any help appreciated!
c++ oop
2
isEven
returns anArray
by value, which is a temporary, so it certainly would invoke a move construction ofb
instead of a copy.
– CoryKramer
Nov 14 '18 at 19:14
4
What sense does it make to have the move constructor allocate a new array? The point is that, given that the source object can be left "empty", you can steal his pointer... Currently your implementation copies and then leaks the source object memory.
– Matteo Italia
Nov 14 '18 at 19:17
2
According ot the rule of 3/5/0 you will need an assignment operator. Note that your move constructor not only doesn't move, it also leaks.
– François Andrieux
Nov 14 '18 at 19:18
1
i dont think this code is "relatively simple". Actually I think this is relatively complicated. Manual memory managment is never simple. Just saying...
– user463035818
Nov 14 '18 at 19:21
1
unless this is one of those "dont do it the c++ way" assignment, you shuold use astd::array
– user463035818
Nov 14 '18 at 19:25
|
show 7 more comments
This is simple program that is supposed to handle a dynamic array of numbers and then to filter out the elements that are even and put them into a new array and then print both arrays on the screen.
This is the header file:
#pragma once
namespace filter
class Array
double *arr;
int n;
public:
Array();
Array(int);
Array(const Array&);
Array(Array&&);
void PutIn();
void PrintOut() const;
Array isEven();
Array filter(const std::function<bool(int)>&) const;
~Array();
;
Then, this is the implementation of functions:
#include <iostream>
#include <functional>
#include "Array.h";
using namespace filter;
using namespace std;
Array::Array() :arr(nullptr), n(0)
Array::Array(int n)
this->n = n;
arr = new double[n];
Array::Array(const Array &a1)
n = a1.n;
arr = new double[n];
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
Array::Array(Array &&a1)
n = a1.n;
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
a1.n = 0;
a1.arr = nullptr;
void Array::PutIn()
cout << "Insert elements:n";
for (int i = 0; i < n; i++)
cin >> arr[i];
void Array::PrintOut() const
cout << "nYour array is :n";
for (int i = 0; i < n; i++)
cout << arr[i] << "t";
Array Array::isEven()
return filter((int x) return x % 2; );
Array Array::filter(const std::function<bool(int)> &f) const
int b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
b++;
Array temp(b);
b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
temp.arr[b] = arr[i];
b++;
return temp;
Array::~Array()
deletearr;
n = 0;
Finally, this is the source code:
#include <iostream>
#include <functional>
#include "Array.h"
using namespace filter;
using namespace std;
int main()
Array a(5);
a.PutIn();
Array b = a.isEven(); //WHY THIS LINE OF CODE INVOKES MOVE CONSTRUCTOR AND NOT COPY CONSTRUCTOR?
a.PrintOut();
b.PrintOut();
getchar();
getchar();
So, as you can see, this is relatively simple program that needs to handle an array with five elements in it entered by user and then to create a new array that consists of even elements of the first array. When i run this, it works fine, however, there is one little thing that i don't understand here.
If you look at the source code, notice the line where i left my comment, that is the line where move constructor is called, but i don't know why. That would mean that a.IsEven() is a RVALUE, since move constructor works with RVALUES, right? Can anyone explain me why this is rvalue and what is the correct way to understand this? Any help appreciated!
c++ oop
This is simple program that is supposed to handle a dynamic array of numbers and then to filter out the elements that are even and put them into a new array and then print both arrays on the screen.
This is the header file:
#pragma once
namespace filter
class Array
double *arr;
int n;
public:
Array();
Array(int);
Array(const Array&);
Array(Array&&);
void PutIn();
void PrintOut() const;
Array isEven();
Array filter(const std::function<bool(int)>&) const;
~Array();
;
Then, this is the implementation of functions:
#include <iostream>
#include <functional>
#include "Array.h";
using namespace filter;
using namespace std;
Array::Array() :arr(nullptr), n(0)
Array::Array(int n)
this->n = n;
arr = new double[n];
Array::Array(const Array &a1)
n = a1.n;
arr = new double[n];
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
Array::Array(Array &&a1)
n = a1.n;
for (int i = 0; i < n; i++)
arr[i] = a1.arr[i];
a1.n = 0;
a1.arr = nullptr;
void Array::PutIn()
cout << "Insert elements:n";
for (int i = 0; i < n; i++)
cin >> arr[i];
void Array::PrintOut() const
cout << "nYour array is :n";
for (int i = 0; i < n; i++)
cout << arr[i] << "t";
Array Array::isEven()
return filter((int x) return x % 2; );
Array Array::filter(const std::function<bool(int)> &f) const
int b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
b++;
Array temp(b);
b = 0;
for (int i = 0; i < n; i++)
if (f(arr[i]) == 0)
temp.arr[b] = arr[i];
b++;
return temp;
Array::~Array()
deletearr;
n = 0;
Finally, this is the source code:
#include <iostream>
#include <functional>
#include "Array.h"
using namespace filter;
using namespace std;
int main()
Array a(5);
a.PutIn();
Array b = a.isEven(); //WHY THIS LINE OF CODE INVOKES MOVE CONSTRUCTOR AND NOT COPY CONSTRUCTOR?
a.PrintOut();
b.PrintOut();
getchar();
getchar();
So, as you can see, this is relatively simple program that needs to handle an array with five elements in it entered by user and then to create a new array that consists of even elements of the first array. When i run this, it works fine, however, there is one little thing that i don't understand here.
If you look at the source code, notice the line where i left my comment, that is the line where move constructor is called, but i don't know why. That would mean that a.IsEven() is a RVALUE, since move constructor works with RVALUES, right? Can anyone explain me why this is rvalue and what is the correct way to understand this? Any help appreciated!
c++ oop
c++ oop
edited Nov 19 '18 at 19:52
cdummie
asked Nov 14 '18 at 19:12
cdummiecdummie
1165
1165
2
isEven
returns anArray
by value, which is a temporary, so it certainly would invoke a move construction ofb
instead of a copy.
– CoryKramer
Nov 14 '18 at 19:14
4
What sense does it make to have the move constructor allocate a new array? The point is that, given that the source object can be left "empty", you can steal his pointer... Currently your implementation copies and then leaks the source object memory.
– Matteo Italia
Nov 14 '18 at 19:17
2
According ot the rule of 3/5/0 you will need an assignment operator. Note that your move constructor not only doesn't move, it also leaks.
– François Andrieux
Nov 14 '18 at 19:18
1
i dont think this code is "relatively simple". Actually I think this is relatively complicated. Manual memory managment is never simple. Just saying...
– user463035818
Nov 14 '18 at 19:21
1
unless this is one of those "dont do it the c++ way" assignment, you shuold use astd::array
– user463035818
Nov 14 '18 at 19:25
|
show 7 more comments
2
isEven
returns anArray
by value, which is a temporary, so it certainly would invoke a move construction ofb
instead of a copy.
– CoryKramer
Nov 14 '18 at 19:14
4
What sense does it make to have the move constructor allocate a new array? The point is that, given that the source object can be left "empty", you can steal his pointer... Currently your implementation copies and then leaks the source object memory.
– Matteo Italia
Nov 14 '18 at 19:17
2
According ot the rule of 3/5/0 you will need an assignment operator. Note that your move constructor not only doesn't move, it also leaks.
– François Andrieux
Nov 14 '18 at 19:18
1
i dont think this code is "relatively simple". Actually I think this is relatively complicated. Manual memory managment is never simple. Just saying...
– user463035818
Nov 14 '18 at 19:21
1
unless this is one of those "dont do it the c++ way" assignment, you shuold use astd::array
– user463035818
Nov 14 '18 at 19:25
2
2
isEven
returns an Array
by value, which is a temporary, so it certainly would invoke a move construction of b
instead of a copy.– CoryKramer
Nov 14 '18 at 19:14
isEven
returns an Array
by value, which is a temporary, so it certainly would invoke a move construction of b
instead of a copy.– CoryKramer
Nov 14 '18 at 19:14
4
4
What sense does it make to have the move constructor allocate a new array? The point is that, given that the source object can be left "empty", you can steal his pointer... Currently your implementation copies and then leaks the source object memory.
– Matteo Italia
Nov 14 '18 at 19:17
What sense does it make to have the move constructor allocate a new array? The point is that, given that the source object can be left "empty", you can steal his pointer... Currently your implementation copies and then leaks the source object memory.
– Matteo Italia
Nov 14 '18 at 19:17
2
2
According ot the rule of 3/5/0 you will need an assignment operator. Note that your move constructor not only doesn't move, it also leaks.
– François Andrieux
Nov 14 '18 at 19:18
According ot the rule of 3/5/0 you will need an assignment operator. Note that your move constructor not only doesn't move, it also leaks.
– François Andrieux
Nov 14 '18 at 19:18
1
1
i dont think this code is "relatively simple". Actually I think this is relatively complicated. Manual memory managment is never simple. Just saying...
– user463035818
Nov 14 '18 at 19:21
i dont think this code is "relatively simple". Actually I think this is relatively complicated. Manual memory managment is never simple. Just saying...
– user463035818
Nov 14 '18 at 19:21
1
1
unless this is one of those "dont do it the c++ way" assignment, you shuold use a
std::array
– user463035818
Nov 14 '18 at 19:25
unless this is one of those "dont do it the c++ way" assignment, you shuold use a
std::array
– user463035818
Nov 14 '18 at 19:25
|
show 7 more comments
1 Answer
1
active
oldest
votes
Your assumption that calling isEven
invokes your move constructor is not in fact correct. Nor does it invoke your copy constructor. Instead, RVO ensures that the object returned is constructed directly at the calling site so neither is required.
Live Demo (which doesn't address any of the flaws in your code mentioned in the comments).
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
You might need them, but not to returnArray
s by value in the vast majority of cases.
– Paul Sanders
Nov 14 '18 at 19:44
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
1
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
1
You're right, MSVC invokes the move constructor in the body ofisEven
. I would call this an MSVC bug - there's no need to do this.
– Paul Sanders
Nov 16 '18 at 18:24
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307246%2fwhy-is-move-constructor-used-over-copy-constructor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your assumption that calling isEven
invokes your move constructor is not in fact correct. Nor does it invoke your copy constructor. Instead, RVO ensures that the object returned is constructed directly at the calling site so neither is required.
Live Demo (which doesn't address any of the flaws in your code mentioned in the comments).
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
You might need them, but not to returnArray
s by value in the vast majority of cases.
– Paul Sanders
Nov 14 '18 at 19:44
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
1
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
1
You're right, MSVC invokes the move constructor in the body ofisEven
. I would call this an MSVC bug - there's no need to do this.
– Paul Sanders
Nov 16 '18 at 18:24
|
show 4 more comments
Your assumption that calling isEven
invokes your move constructor is not in fact correct. Nor does it invoke your copy constructor. Instead, RVO ensures that the object returned is constructed directly at the calling site so neither is required.
Live Demo (which doesn't address any of the flaws in your code mentioned in the comments).
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
You might need them, but not to returnArray
s by value in the vast majority of cases.
– Paul Sanders
Nov 14 '18 at 19:44
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
1
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
1
You're right, MSVC invokes the move constructor in the body ofisEven
. I would call this an MSVC bug - there's no need to do this.
– Paul Sanders
Nov 16 '18 at 18:24
|
show 4 more comments
Your assumption that calling isEven
invokes your move constructor is not in fact correct. Nor does it invoke your copy constructor. Instead, RVO ensures that the object returned is constructed directly at the calling site so neither is required.
Live Demo (which doesn't address any of the flaws in your code mentioned in the comments).
Your assumption that calling isEven
invokes your move constructor is not in fact correct. Nor does it invoke your copy constructor. Instead, RVO ensures that the object returned is constructed directly at the calling site so neither is required.
Live Demo (which doesn't address any of the flaws in your code mentioned in the comments).
answered Nov 14 '18 at 19:36
Paul SandersPaul Sanders
5,2662621
5,2662621
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
You might need them, but not to returnArray
s by value in the vast majority of cases.
– Paul Sanders
Nov 14 '18 at 19:44
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
1
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
1
You're right, MSVC invokes the move constructor in the body ofisEven
. I would call this an MSVC bug - there's no need to do this.
– Paul Sanders
Nov 16 '18 at 18:24
|
show 4 more comments
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
You might need them, but not to returnArray
s by value in the vast majority of cases.
– Paul Sanders
Nov 14 '18 at 19:44
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
1
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
1
You're right, MSVC invokes the move constructor in the body ofisEven
. I would call this an MSVC bug - there's no need to do this.
– Paul Sanders
Nov 16 '18 at 18:24
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
Are you suggesting that i don't even need move and copy constructor here? I don't think i understand.
– cdummie
Nov 14 '18 at 19:42
You might need them, but not to return
Array
s by value in the vast majority of cases.– Paul Sanders
Nov 14 '18 at 19:44
You might need them, but not to return
Array
s by value in the vast majority of cases.– Paul Sanders
Nov 14 '18 at 19:44
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
I see, i thought it was invoked, since when i was debugging it, it appeared as if the move constructor was called.
– cdummie
Nov 14 '18 at 19:46
1
1
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
Not really - read the first paragraph of the 'RVO' (aka copy elision) link I provided - copy elision is an important optimisation when returning objects by value, I'm surprised the other commentors didn't mention it.
– Paul Sanders
Nov 14 '18 at 19:58
1
1
You're right, MSVC invokes the move constructor in the body of
isEven
. I would call this an MSVC bug - there's no need to do this.– Paul Sanders
Nov 16 '18 at 18:24
You're right, MSVC invokes the move constructor in the body of
isEven
. I would call this an MSVC bug - there's no need to do this.– Paul Sanders
Nov 16 '18 at 18:24
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307246%2fwhy-is-move-constructor-used-over-copy-constructor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
isEven
returns anArray
by value, which is a temporary, so it certainly would invoke a move construction ofb
instead of a copy.– CoryKramer
Nov 14 '18 at 19:14
4
What sense does it make to have the move constructor allocate a new array? The point is that, given that the source object can be left "empty", you can steal his pointer... Currently your implementation copies and then leaks the source object memory.
– Matteo Italia
Nov 14 '18 at 19:17
2
According ot the rule of 3/5/0 you will need an assignment operator. Note that your move constructor not only doesn't move, it also leaks.
– François Andrieux
Nov 14 '18 at 19:18
1
i dont think this code is "relatively simple". Actually I think this is relatively complicated. Manual memory managment is never simple. Just saying...
– user463035818
Nov 14 '18 at 19:21
1
unless this is one of those "dont do it the c++ way" assignment, you shuold use a
std::array
– user463035818
Nov 14 '18 at 19:25