Destructors

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
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..
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
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!!!
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
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
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/)
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;
 
	}
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...
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.
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
Something to read ...
GotW series: http://herbsutter.com/gotw/
(older) http://www.gotw.ca/gotw/
Many thanks!!!
Topic archived. No new replies allowed.