Destructors

May 3, 2015 at 7:43am
Hello!
Tried to write simpelst code for destructor use.
Plese, where is the error?

If I want to need destructor, then I need dynamic alocated element.
Thanks for any help! (http://www.learncpp.com/cpp-tutorial/86-destructors/)

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
#include<iostream>
using namespace std;
 
 
class mmm{
	private:
	int* a;
 
	public:
 
	//constructor: 
	mmm(int *b){   
		a=new int(3);
	}
 
	//destructor:
	~mmm(){delete a;}
 
	int* getNumber(){return a;}
 
};
 
int main(){
	mmm mobj1;
	cout<<mobj1.getNumber()<<endl;
	return 0;
 
	}
Last edited on May 3, 2015 at 8:10am
May 3, 2015 at 7:44am
erros:
prog.cpp: In function 'int main()':
prog.cpp:24:13: error: invalid conversion from 'int' to 'int*' [-fpermissive]
mmm mobj1(6);
^
prog.cpp:12:2: note: initializing argument 1 of 'mmm::mmm(int*)'
mmm(int *b){

p.s I copied literarily example from tutorial...confused..
May 3, 2015 at 8:00am
Your destructor is fine. The issue is that your constructor takes a pointer to int as a parameter but you're passing in 6.
Last edited on May 3, 2015 at 8:01am
May 3, 2015 at 8:14am
Hello, thanks, that is what I was lokung at, but the hell, I have DYNAMIC memory , (otherwise I would not need an destructor, woudl I)?

So I definitely cannot write a place in memory as a value (or maybe I even can, but I not intend!)

I wanted int * a to be a pointer for dynamic memory allocation, so how to manage this, please?
Thanks!!!
May 3, 2015 at 8:20am
Why does the constructor have parameters if the value of it is never used inside the constructor? Even if you want to have parameters, b doesn't have to be a pointer just because a is.
Last edited on May 3, 2015 at 8:21am
May 3, 2015 at 8:45am
Line 24 seems to call default destructor (one that takes no parameters), but the class mmm does not have one. The compiler does not implicitly generate default constructor, because the class defines explicit constructor (with one parameter).


Line 25 prints a memory address, not a value.


When a class manages dynamic memory, it is not enough to write explicit destructor. You have to consider these too:
* copy constructor
* copy assignment
* move constructor
* move assignment
* default constructor
May 3, 2015 at 9:23am
That is a problem, I am searching for sntax itself. (=without something missing!)

Here is an example, which does not have it all (and btw this example DOES not work ,so I wonder what I miss to make proper code and where to find syntaks or protopype)

(http://www.learncpp.com/cpp-tutorial/86-destructors/)
May 3, 2015 at 9:34am
Tell me "bravo!"
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
#include<iostream>
using namespace std;
 
 
class mmm{
	private:
	int* a;
 
	public:
 
	//constructor: 
	mmm(){   
		a=new int();
		*a=3;
 
	}
 
	//destructor:
	~mmm(){delete a;}
 
	int* getNumber(){return a;}
	int getValue(){return *a;}
 
};
 
int main(){
	mmm mobj1;
	cout<<mobj1.getNumber()<<endl;
	cout<<mobj1.getValue()<<endl;
	return 0;
 
	}
May 3, 2015 at 9:46am
Is that theorethically ok, if not why not? So, is it right that I was using destructor because I operated with variable *a, containting value 3, which was placed on
0xdeadbeaf, and if I fnished the code without destructor, I would have memory leak, right?


So, next step would be to write code whose type would be mmm(=class type).

Please, any ideas for pseudocode? (tried to google, but couldn't, do I miss some keywords?)..

Anyhow, what is that destructor (~mmm) destructing? It is acutaly a variable, that it is deleting. We can do an object using that variable, but deleted is what? would say- variable, and CONSEQUENTLY but ONLY consequently object mobj1...am I right? So, ~mmm IS deleting the object mobj1, but CONSEQUENTLY, not directly...hope U understand what I ment, cause now I am trying to write code using class type objects to be stored on the heap...
May 3, 2015 at 9:47am
I copied literarily example from tutorial...confused..

Your code is not a verbatim copy of either program in the tutorial that you did link to.

You should pay much more attention to details.
May 3, 2015 at 9:56am
Please ,any good links (and books) are wellcome...I am trying to find out what particular details in codes serve for ...do I miss sth in my example?

This:
http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm
example works withou changing anything, but I would rather say, it is an example for destructor which is NOT NECESSARRY, is it?

I tried to make a code in which destructor would prevent memory leak...so please tell me all U think that is not good represented in my code up...thanks!!!
Last edited on May 3, 2015 at 9:56am
May 3, 2015 at 10:47am
Something to read ...
GotW series: http://herbsutter.com/gotw/
(older) http://www.gotw.ca/gotw/
May 3, 2015 at 12:01pm
Many thanks!!!
Topic archived. No new replies allowed.