Using destructor

D

desktop

I have this class:

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;
};

int main() {

MyArray myarr(5);
return 0;
}

But when are the destructor called?
 
D

Devon Null

desktop said:
I have this class:

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;
};

int main() {

MyArray myarr(5);
return 0;
}

But when are the destructor called?

The destructor is called implicitly when the object is distroyed - how
it is destroyed does not matter.
 
D

Daniel T.

desktop said:
I have this class:

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;
};

int main() {

MyArray myarr(5);
return 0;
}

But when are the destructor called?

When "myarr" goes out of scope.
 
S

Salt_Peter

I have this class:

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

MyArray(const int a) : data(new int[a]) { }
~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;

};

int main() {

MyArray myarr(5);
return 0;

}

But when are the destructor called?

The destructor is invoked when the object goes out of scope. In this
case it happens when main returns. Add an output (std::cout <<
"~MyArray()";) to your d~tor to see it.

Did you mean to write int& MyArray::eek:perator[](int a) { ... } ?
 
A

archimed7592

desktop said:
I have this class:
class MyArray {
public:
MyArray(int a) : data(new int[a]){}
~MyArray() {delete[] data;}
int& operator()(int a){
return data[a];
}
int operator()(int a) const{
return data[a];
}
private:
int* data;
};
int main() {
MyArray myarr(5);
return 0;
}
But when are the destructor called?

The destructor is called implicitly when the object is distroyed - how
it is destroyed does not matter.

btw, you can invoke destructor explicitly:
myarr.~MyArr();
note: in member functions you should write 'this->~MyArray();'. not
'~MyArray()' (without 'this->')
 
R

Rolf Magnus

archimed7592 said:
desktop said:
I have this class:
class MyArray {
public:
MyArray(int a) : data(new int[a]){}
~MyArray() {delete[] data;}
int& operator()(int a){
return data[a];
}
int operator()(int a) const{
return data[a];
}
private:
int* data;
};
int main() {
MyArray myarr(5);
return 0;
}
But when are the destructor called?

The destructor is called implicitly when the object is distroyed - how
it is destroyed does not matter.

btw, you can invoke destructor explicitly:
myarr.~MyArr();
note: in member functions you should write 'this->~MyArray();'. not
'~MyArray()' (without 'this->')

Note however that this is almost never a good idea.
 
S

Salt_Peter

desktop said:
I have this class:
class MyArray {
public:
MyArray(int a) : data(new int[a]){}
~MyArray() {delete[] data;}
int& operator()(int a){
return data[a];
}
int operator()(int a) const{
return data[a];
}
private:
int* data;
};
int main() {
MyArray myarr(5);
return 0;
}
But when are the destructor called?
The destructor is called implicitly when the object is distroyed - how
it is destroyed does not matter.

btw, you can invoke destructor explicitly:

and you can also jump off a skyscraper without a parachute.
myarr.~MyArr();
note: in member functions you should write 'this->~MyArray();'. not
'~MyArray()' (without 'this->')

If a user of the class needs to manage the lifetime of an object, he/
she should be doing it through dynamic allocation using smart pointers
or new/delete.
 
J

Jim Langston

desktop said:
I have this class:

class MyArray {
public:
MyArray(int a) : data(new int[a]){}

~MyArray() {delete[] data;}

int& operator()(int a){
return data[a];
}

int operator()(int a) const{
return data[a];
}

private:
int* data;
};

int main() {

MyArray myarr(5);
return 0;

Here is where the destructor is called. This return statement is exiting
the function main, myarr goes out of scope, so it's destructor is called.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top