Position of the destructor in class ? C++

You don't have to read all the program. I put the code for destructor in a box which can be easily found. I just want to know if the position of a destuctor does matter in the code ? Like what happens if I place it at the end of my class ?

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
#include <iostream>
#include "cmpt_error.h"
#include <cassert>

using namespace std;


class int_vec {
private:
    int capacity; // length of underlying array
    int* arr;     // pointer to the underlying array
    int size;     // # of elements in this int_vec from user's perspective

    void resize(int new_cap) {
        //...
    }
        delete[] arr;                     // delete old arr
        arr = new_arr;                    // assign new_arr
    }

public:
    // Default constructor (takes no parameters)
    int_vec()
    : capacity(10), arr(new int[capacity]), size(0)
    { }

    int_vec(int sz, int fill_value){
        //...
    }
    

    // Copy constructor
    int_vec(const int_vec& other){
        //...
    }
    
    //=============Destructor================
    ~int_vec() {
        //...
    }
    //=======================================
    int get_size() const {
        //...
    }

    int get_capacity() const {
        //...
    }

    int get(int i) const {
        //...
    }

    void set(int i, int x) {
        //...
    }

    int& operator[](int i) {
        //...
    }

    void print() const {
        //...
    }

    void println() const {
        //...
    }

    void append(int x) {
        //...
    }


    int_vec& operator=(const int_vec& other) {
        //...
     }
    
}; 
Last edited on
> Like what happens if I place it at the end of my class ?

It can be placed anywhere in the class.
For a class like this (not designed for inheritance), the destructor should be public.
For any class, the destructor should be fail-safe (should not throw).
No, it does not matter where in the class you put the destructor.
So you mean the constructors construct the values of member variables, and then they destruct stuff ? I was afraid placing destructor in the middle of my program might make the program's member functions in public to not be able to access the member variables' values.
...might make the program's member functions in public to not be able to...

Constructors and destructors are functions, even if their syntax slightly diverges from the classic one of functions because they can't return any value, so it's not possible to specify a return value.
Like any function, it doesn't matter neither where you declare it nor where you define it, but only where you invoke it. The constructor is automatically invoked when you initialize an instance of its class, the destructor when that instance is cancelled.
I think JLBorges gave you the best advice.
Topic archived. No new replies allowed.