How to use destructor in my case

Below is my code, I want to know how to use destructor in my case and how to implement in my code. Thank you for helping.

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
  class SM
{
	node* head;
	node* tail;
	node* v;
	node* newNode;
public:
	SM() {}
	SM(int row, int column) {
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < column; j++) {
				if (i == 0 && j == 0)
				{
					v = new node;
					v->sparseMatrix[i][j] = 0;
					v->prev = NULL;
					head = v;
					tail = v;

				}
				else if (i == row - 1 && column - 1)
				{
					v = new node;
					v->sparseMatrix[i][j] = 0;
					v->prev = tail;
					tail->next = v;
					tail = v;
					tail->next = NULL;
				}
				else
				{
					v = new node;
					v->sparseMatrix[i][j] = 0;
					v->prev = tail;
					tail->next = v;
					tail = v;
				}
			}
		}
	}

	SM * addSM() {}
	SM * addSM(SM sm1, SM sm2, SM sm3) {
		sm1.newNode = sm1.head;
		sm2.newNode = sm2.head;
		sm3.newNode = sm3.head;
		for (int i = 0; i < initrow; i++)
		{
			cout << "\t";
			for (int j = 0; j < initcolumn; j++) {
				sm3.newNode->sparseMatrix[i][j] = sm1.newNode->sparseMatrix[i][j] + sm2.newNode->sparseMatrix[i][j];
				cout << sm3.newNode->sparseMatrix[i][j] << "\t";
				sm1.newNode = sm1.newNode->next;
				sm2.newNode = sm2.newNode->next;
				sm3.newNode = sm3.newNode->next;
			}
			cout << endl;
		}
		cout << endl;

		return 0;
	}

	~SM() {}
	~SM() {
		cout << "Destrutor??";

	}
	void readElement();
	void printMatrix();
};
If you would have that in a program that uses it -- for example:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using std::cout;
using std::endl;

// insert your code here

int main() {
  SM fubar;
  return 0;
}

Would that compile (i.e. without errors)?
Would there be any compiler warnings?
Would the program run, or crash?


What is the purpose of the type SM? What idea does it implement?

The owner is responsible of orderly deallocation of owned resources. That involves both destruction and assignment (copy/move). What resources does an SM object own?

Does the example program above "use destructor"? If yes, where? If no, should it?
Generally you'd iterate through your custom structure and delete every new thing you created.
But yeah, as keskiverto said, this likely doesn't compile (love those posts). 'initrow' and 'initcolumn' are especially absent.
in words,
it looks like you need to delete the entire linked list (node by node, since you allocated it this way, classic linked list destroy function) and set head, tail, and possibly other pointers to null as you unwind. It can be done various ways but recursion down to the last node and then destroy as you back out is fairly simple code.

Totally none of my business and an aside, but typically the POINT of a sparse matrix scheme is to avoid storing zeroed out elements. I can't tell for sure but it looks like you are storing those ?! Generally, you store (row, column, value) and if a location is not in your storage, its zero, but unstored to save space. There are any number of nifty schemes to store these, but that is the concept.



Last edited on
Topic archived. No new replies allowed.