By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Using destructor

P: n/a
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?
May 28 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
desktop wrote:
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.
May 28 '07 #2

P: n/a
desktop <ff*@sss.comwrote:
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.
May 28 '07 #3

P: n/a
On May 28, 10:05 am, desktop <f...@sss.comwrote:
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::operator[](int a) { ... } ?

May 28 '07 #4

P: n/a
On May 28, 6:11 pm, Devon Null <theronnights...@xgmailx.comwrote:
desktop wrote:
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->')

May 28 '07 #5

P: n/a
archimed7592 wrote:
On May 28, 6:11 pm, Devon Null <theronnights...@xgmailx.comwrote:
>desktop wrote:
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.

May 28 '07 #6

P: n/a
On May 28, 3:02 pm, archimed7592 <archimed7...@gmail.comwrote:
On May 28, 6:11 pm, Devon Null <theronnights...@xgmailx.comwrote:
desktop wrote:
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.

May 28 '07 #7

P: n/a

"desktop" <ff*@sss.comwrote in message
news:f3**********@news.net.uni-c.dk...
>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.
}

But when are the destructor called?

May 29 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.