Help needed

Help, need to add both input together,somehow when i do s1.display(); nothing came out; isnt data suppose to save my value?

input_a =
1 -2 13 4

4 -10 33 21

input_b =
10 -22 18 14
24 7 133 210

cant use 2d array or vector..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

int main(){

    Matrix n1;
    Matrix n2;

    n1.populate("input_a.txt",2,4);
    n1.display();

    n2.populate("input_b.txt",2,4);
    n2.display();

    Matrix s1;

    s1=n1+n2;

    s1.display();






}




#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

Matrix::Matrix(){

    row=0;
    col=0;

}

Matrix::Matrix(string fn,int r,int c){

    row=r;
    col=c;




}

Matrix::Matrix(const Matrix &m){

    col=m.col;
    row=m.col;

}
Matrix::~Matrix(){

        delete [] data;




}

int Matrix::getRow(){

    return row;


}

int Matrix::getCol(){

    return col;

}

void Matrix::populate(string fn,int r,int c){

    row=r;
    col=c;

    ifstream fin;  //file input stream

    fin.open(fn.c_str());
    int count=0;

    while (fin>>fn){//read until eof
        data[count] = atof(fn.c_str());
        count++;
    }


    fin.close();



}

void Matrix::set(int r,int c,float v[20]){

    row=r;
    col=c;

    int z=0;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            data[z]=v[z];
            cout<<data[z]<<" ";
            z++;
        }
        cout<<endl;
    }



}

float Matrix::get(int r,int c){




}

void Matrix::display(){

    int z = 0;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cout<<data[z]<<" ";
            z++;
        }
    cout<<endl;
    }

    cout<<data[0]<<endl;

}

Matrix& Matrix::operator=(const Matrix& other){



}

Matrix& Matrix::operator+(const Matrix& other){

    int z = 0;

    Matrix* sum = new Matrix ();
    float temp[20];

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            temp[z]=data[z]+other.data[z];
            //sum->data[z]+=other.data[z];
            z++;
        }
    }

    //   sum->temp[z];
    sum->set(row,col,temp);


    return *sum;



}

Matrix& Matrix::operator*(const Matrix& other){





}



Last edited on
> #include "matrix.h"
.oi


> delete [] data;
¿where do you initialise `data'?


> Matrix* sum = new Matrix ();
http://www.cplusplus.com/forum/general/138037/#msg731921 (except that you don't delete it, you just leak)
the data thing is not completed yet, i just want it to be work first.

lets say i do

1
2
3
4
5
6
7
8
9
10
Matrix n1;
Matrix n2;

Matrix s1;

s1=n1+n2;

s1.display();

my problem is that it i cant display s1.display datas?
here is my .h file..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef MATRIX_H
#define MATRIX_H
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class Matrix{

	private:

		int row;
		int col;
		float data[20];

	public:

		Matrix();
		Matrix(string fn,int r,int c);
		Matrix(const Matrix &m);
		~Matrix();
		int getRow();
		int getCol();
		void populate(string fn,int r,int c);
		void set(int r,int c,float v[20]);
		float get(int r,int c);
		void display();
		Matrix& operator=(const Matrix& other);
		Matrix& operator+(const Matrix& other);
		Matrix& operator*(const Matrix& other);
















};
#endif

closed account (48T7M4Gy)
.
Last edited on
Your member "data" is a statically allocated array, not dynamic using new[]. Therefore it will go out of existence when the Matrix it's part of does, and your destructor must not call "delete[]" on it!

Line 148 looks strange. Why print out the 0th element again? It not going to cause program failure tho.

The problem lies in your operator=() its not implemented, so when you assign s1 to that equation on line 21, s1 is just left high and dry, with the row and column still left at 0 (in default c-tor). Your copy-constructor is also incomplete because it ignores the values in the "data" array.

Operator+, operator*, get() display(), getCol(), and getRow() should all be designated as const member functions. Operator+ and operator* should return by value (not by reference) also.

Your operator+() has a few things wrong too. You allocate a new Matrix object with "new" but never deallocate it with "delete". Within its body, you'll want to create a placeholder Matrix, make its data array = to the current matrix + the parameter being added. Then return that placeholder matrix. Your operator* will follow the same principle.

Your matrix class also faces a design flaw. You simply accept any value for row and column sizes, and use them to drive all your loops, but your array has a predetermined number of elements (ie, 20). If I try to "set()" dimensions like 4x6 (=24) I will, without warning of anykind, get out-of-bounds array accesses.
closed account (48T7M4Gy)
..
Last edited on
closed account (48T7M4Gy)
...
Last edited on
Really sorry for the messy code..

As to why the operator= is not done yet is because i dont know what to do with it, i will try to find out more. Also yes, the float data[20]; is a flaw, im told to use float *data; but when i compile it just stop working. Any place i can learn more about operator=? in class? Any help would be appreciated. thanks!

I put the cout at void Matrix::set(int r,int c,float v[20]) thats why it can print out value, it just that the s1 dont store value by itself. Need more guidance on the operator= thought
Last edited on
closed account (48T7M4Gy)
..
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef MATRIX_H
#define MATRIX_H
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class Matrix{

	private:

		int row;
		int col;
		float data[20];

	public:

		Matrix();
		Matrix(string fn,int r,int c);
		Matrix(const Matrix &m);
		~Matrix();
		int getRow();
		int getCol();
		void populate(string fn,int r,int c);
		void set(int r,int c,float v[20]);
		float get(int r,int c);
		void display();
		Matrix& operator=(const Matrix& other);
		Matrix& operator+(const Matrix& other);
		Matrix& operator*(const Matrix& other);
		
};
#endif



#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

Matrix::Matrix(){

    row=0;
    col=0;
    //data={0};

}

Matrix::Matrix(string fn,int r,int c){

    row=r;
    col=c;

}

Matrix::Matrix(const Matrix &m){

    col=m.col;
    row=m.col;

}
Matrix::~Matrix(){

        delete [] data;

}

int Matrix::getRow(){

    return row;


}

int Matrix::getCol(){

    return col;

}

void Matrix::populate(string fn,int r,int c){

    row=r;
    col=c;

    ifstream fin;

    fin.open(fn.c_str());
    int count=0;

    while (fin>>fn){
        data[count] = atof(fn.c_str());
        count++;
    }

    fin.close();

}

void Matrix::set(int r,int c,float v[20]){

    row=r;
    col=c;

    int z=0;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            data[z]=v[z];
            z++;
        }
    }

}

float Matrix::get(int r,int c){




}

void Matrix::display(){

    int z = 0;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cout<<data[z]<<" ";
            z++;
        }
    cout<<endl;
    }
}

Matrix& Matrix::operator=(const Matrix& other){

    delete data;
    int z=0;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            data[z]=other.data[z];
            z++;
        }
    }

    row=other.row;
    col=other.col;

    return *this;


}

Matrix& Matrix::operator+(const Matrix& other){

    int z = 0;

    Matrix* sum = new Matrix ();
    float temp[20];

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            temp[z]=data[z]+other.data[z];
            z++;
        }
    }

    sum->set(row,col,temp);

    return *sum;

}

Matrix& Matrix::operator*(const Matrix& other){





}


#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

int main(){

    Matrix n1;
    Matrix n2;

    n1.populate("input_a.txt",2,4);
    n1.display();

    n2.populate("input_b.txt",2,4);
    n2.display();

    Matrix s1=Matrix("NULL",2,4);

    s1=n1+n2;

    s1.display();






}




Thanks alot magnum pi! it seriously help me alot, went to look up on move constructor, now i can do the display, really thanks alot! Just how do i use float* data?

edit : found out that if i use float* data, the error lies on the populate function. any idea? the matrix.o just stop working.
Last edited on
You're welcome! That is better, but there are still a few things wrong. The reason the populate() func would fail, as written if you change from float[20] to a float* is that when you read in the file, you stored values at "data[z]" repeatedly, when data didn't point to an array, it contained garbage.

A move constructor/operator= is indeed desirable whenever a datatype contains some kind of resource (such as dynamic memory) that should be "passed along" when constructing/assigning from an rvalue. Look at the comments in this revised code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef MATRIX_H
#define MATRIX_H
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class Matrix{

	private:
		int row;
		int col;
		float data*;

	public:
              //TODO: Add "const" to signal const member functions.
		Matrix();
		Matrix(int r, int c);  //Get rid of unused string param here.
		Matrix(const Matrix &m);
                Matrix(Matrix&& m);  //Move c-tor.

		~Matrix();
		int getRow();
		int getCol();
		void  populate(string fn,int r,int c);
		void  set(int r,int c,float v[20]);
		float  get(int r,int c);
		void  display();
		Matrix& operator=(const Matrix& other);
		Matrix& operator=(Matrix&& other);  //Move operator=

               //These must not use "new Matrix" b/c that leads to memory leak!
               //Return a Matrix by value.
		Matrix operator+(const Matrix& other) const;
		Matrix operator*(const Matrix& other) const;
		
};
#endif



#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

Matrix::Matrix(){
    data=nullptr;
    row=0;
    col=0;
}

Matrix::Matrix(int r,int c)  //Get rid of unused string param here.
{
   data = new float[r * c];  //Must make sure array has the number of elements our other members say it does.
   row = r;
   col = c;
}

Matrix::Matrix(const Matrix& m)
{
    data = new float[m.row * m.col];  //Allocate space we need.
    col = m.col;
    row = m.col;
  //TODO: Copy data from "m".
}
Matrix::Matrix(Matrix&& m)
{
    data = m.data;
    row = m.row;
    col = m.col;
   //Leave "m" empty, but members should still validly reflect its state...
    m.data = nullptr;
    m.row = m.col = 0;
}
Matrix::~Matrix(){
        delete [] data;
}

int Matrix::getRow(){
    return row;
}

int Matrix::getCol(){
    return col;
}

void Matrix::populate(string fn, int r, int c)
{
    ifstream fin( fn );  //Can open file using non-default c-tor.
  //Also since C++11, can open file-streams from string-name without needing c_str.

    int desiredLength = (r * c);
    int count=0;
    float temp;

  //Phase 1: Go thru file, calculate needed number of values. Stop if file contains more values than what our dimensions allow.
    while (fin>>temp) {
        count++;
        if (count > desiredLength)
           break;
    }
    fin.clear();  //Clear error flags so we can use stream again.
    fin.seekg(0, ios::beg);  //Go back to the beginning point, offseted by 0 (meaning the very beginning).

  //Phase 2: Make sure our array has exactly right number of elements.
    if ((row * col) != desiredLength) {  //Old size doesn't work anymore
       delete[] data;
       data = new float[desiredLength];
    }
    row = r;  //Update dimension sizes.
    col = c;

  //Phase 3: Go thru file again, storing the values read. Don't try to store more elements than exist in our array!
    count = 0;  //Fill array at this index, start at 0.
    //TODO: See if you can do this based on Phase 3 description.

//    fin.close();  //Not needed, file closes itself on destruction.
}

void Matrix::set(int r, int c, float v*){
//TODO: This func needs fixing, because your data array is dynamic now...
}

float Matrix::get(int r,int c){

}

void Matrix::display()
{
    int z = 0;
    for(int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cout << data[z]<<" ";
            z++;
        }
        cout<<endl;
    }
}

//Move assignment operator.
Matrix& Matrix::operator=(Matrix&& other)
{
   //TODO: See if you can figure this out!
   //Remember b/c its an operator=, you must check for self-assignment.
   //Don't memory leak! If data already points to something, delete[] it before reassigning data!
   //Unlike the copy operator=, you can modify the members of "other" here.
   //The goal is to "pass on" ownership of resources owned by "other" and "give" them to object referenced by *this.
   //Be sure "other" is left in a valid state after this function ends, b/c the d-tor for "other" will still be run.
   return( *this );  //Last line.
}

//Copy-assignment operator.
Matrix& Matrix::operator=(const Matrix& other)
{
   if (this != &other) {  //Important for an operator=()
//You called wrong delete! Must be delete[] b/c it points to data obtained by calling new[]. Even for int/float types, this is an important point!
     int otherSize = (other.row * other.col);
     if (otherSize != (row * col)) {  //Don't perform a delete[]/new[] (expensive) unless size is wrong and we need to.
        delete[] data;
        data = new float[otherSize];
     }
     row = other.row;
     col = other.col;

     for (int i = 0; i < otherSize; i++){
         data[z] = other.data[z];
     }
   }  //End if (this != ...)
    return *this;
}

//Return type must be a "Matrix" not a "Matrix&"
Matrix Matrix::operator+(const Matrix& other) const  //Designate as const!
{
    int z = 0;
    Matrix sum(row, col);  //Construct using non-default c-tor.

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            sum.data[z] = data[z] + other.data[z];
            z++;
        }
    }
    return( sum );  //Now calls "Matrix(Matrix&&)" instead of "Matrix(const Matrix&)" b/c you've written it!
}

//Ditto operator*()
Matrix Matrix::operator*(const Matrix& other) const {
   //TODO: Finish this!
}


#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

int main(){

    Matrix n1;
    Matrix n2;

    n1.populate("input_a.txt",2,4);
    n1.display();

    n2.populate("input_b.txt",2,4);
    n2.display();

    Matrix s1 = Matrix(2, 4);

    s1 = n1 + n2;  //Calls operator=(Matrix&&) on s1. B/c (n1 + n2) produces a temporary.

    s1.display();

}
Last edited on
closed account (48T7M4Gy)
.
Last edited on
magnum pi, i cant seem to use nullptr, it say not declared. also cannot use this code Matrix& Matrix::operator=(Matrix&& other) cant have two &
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

#ifndef MATRIX_H
#define MATRIX_H
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class Matrix{

	private:

		int row;
		int col;
		float *data;

	public:

		Matrix();
		Matrix(int r,int c);
		Matrix(const Matrix &m);

		~Matrix();
		int getRow()const;
		int getCol()const;
		void populate(string fn,int r,int c);
		void set(int r,int c,float*v);
		float get(int r,int c);
		void display();
		Matrix& operator=(const Matrix& other);
		Matrix operator+(const Matrix& other);
		Matrix operator*(const Matrix& other);
};
#endif


#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

int main(){

    Matrix n1;
    Matrix n2;

    n1.populate("input_a.txt",2,4);
    n1.display();

    n2.populate("input_b.txt",2,4);
    n2.display();

    Matrix s1=Matrix(2,4);

    s1=n1+n2;

    s1.display();

    Matrix s2;

    float v[] = { 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5 };
    s2.set(5,4,&v[0]);

    s2.display();

    Matrix n3;
    Matrix n4;

    n3.populate("input_c.txt",2,2);
    n3.display();

    n4.populate("input_d.txt",2,2);
    n4.display();

    Matrix s3=Matrix(2,2);

    s3=n3*n4;

    s3.display();


}




#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "matrix.h"
using namespace std;

Matrix::Matrix(){

    row=0;
    col=0;
    data = new float[0];
}

Matrix::Matrix(int r,int c){

    data = new float [r*c];
    row=r;
    col=c;

    for (int i = 0; i < row * col; i++){
        data[i]=0;
    }

}

Matrix::Matrix(const Matrix &m){

    data = new float [m.row * m.col];
    col=m.col;
    row=m.col;

    int z=0;

    for (int i = 0; i < m.row; i++){
        for (int j = 0; j < m.col; j++){
            data[z]=m.data[z];
            z++;
        }
    }
}

Matrix::~Matrix(){

        delete [] data;

}

int Matrix::getRow()const{

    return row;

}

int Matrix::getCol()const{

    return col;

}

void Matrix::populate(string fn,int r,int c){

    int length = r*c;
    int count = 0;
    float temp;

    ifstream fin;

    fin.open(fn.c_str());

    while (fin>>temp) {
        count++;
        if (count > length)
           break;
    }
    fin.clear();
    fin.seekg(0, ios::beg);

    if ((row * col) != length) {
       delete[] data;
       data = new float[length];
    }

    data = new float[length];
    row=r;
    col=c;
    count=0;

    while (fin>>fn){
        data[count] = atof(fn.c_str());
        count++;
    }
    fin.close();

}

void Matrix::set(int r,int c,float* v){

    row=r;
    col=c;

    data = new float [row*col];


    for (int i = 0; i < r*c; i++){
            data[i]=v[i];
        }
}

float Matrix::get(int r,int c){




}

void Matrix::display(){

    int z = 0;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cout<<data[z]<<" ";
            z++;
        }
    cout<<endl;
    }

    cout<<endl;
}

Matrix& Matrix::operator=(const Matrix& other){

    row=other.row;
    col=other.col;

    for (int i = 0; i < row*col; i++){
            data[i]=other.data[i];
    }

    return *this;

}

Matrix Matrix::operator+(const Matrix& other){

    int z = 0;
    int r = other.getRow();
    int c = other.getCol();

    Matrix sum(r,c);


    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            sum.data[z]=data[z]+other.data[z];
            z++;
        }
    }

    return sum;

}

Matrix Matrix::operator*(const Matrix& other){


    int z = 0;
    int r = other.getRow();
    int c = other.getCol();

    Matrix multi(r,c);

    float temp;

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < other.col; j++) {
            temp=0;
            for (int k = 0; k < other.row; k++)
                temp = temp + data[i * col + k] * other.data[k * other.col + j];
                multi.data[i*c+j]=temp;
        }

    }

    return multi;

}





Thank you both for the guidance! @kermot really appreciate your code, it helps me understand alot. @ magnum pi, i read through all the comments and some codes that i never see before in my class, really help me know C++ more! thanks! But some is still not that clear to me like why move assignment operator is needed, as i already have a copy =? Thanks again!
Get rid of line 172, b/c if the if statement on line 167 executes, you'll have a memory leak of the floats allocated on line 169. And line 190 should be replaced with an if statement similar to that on 167, otherwise a memory leak would result if data points to something. A similar if statement is needed in your operator=(const Matrix&) func. Otherwise your next for-loop may write to non-existent elements. Good work tho! Huge improvement from your original code! :)

If you got a compiler error message concerning "nullptr" and having 2 "&" in your operator=(), that indicates your compiler isn't supporting features added in C++11. (Move operators and nullptr were both added in C++11.) What compiler do you use? Is it possible that it just needs a compiler option for it? I know Orwell Dev (the one I primarily use) requires such...

Move operators are not strictly "needed", but it's a missed opportunity for performance if its left out. There's plenty of good explanations out on the web of what move operators are, and how they differ so I'll attempt to give the short version. Having both a move operator= and copy operator= allows your code to distinguish between being assigned to a named variable (in which case I should perform a full-copy of, and not modify, the "other" param), or a temporary object (such as returned by your operator+). In the latter case, the temporary should have its resources "stolen" and acquired by *this. That's FAR more efficient than a full-copy, and the temporary is about to go out of existence anyway, so why not steal from it? Don't forget to make sure the temporary is in a valid state after the "stealing" because it WILL still have its destructor run later.

But you do need C++11 support to have move operators...

Last edited on
Topic archived. No new replies allowed.