|
-
March 1st, 2004, 07:49 AM
#1
Confusion?
if i declared something like this
int myValue..........
.
.
.
.
will this will call destructor when it goes out of scope?
Thanks
Vishal
-
March 1st, 2004, 08:18 AM
#2
Hi Vishal,
For generic data type object (i.e myvalue), no destructor is called. At least we can control to that level.
For any other user defined data type, the destructor is called when object goes out of scope.
Hope so I understood your question clearly...
Regards
Nagesh
-
March 1st, 2004, 10:50 AM
#3
No constructors or destructors are called for this type of value (int, float, char, ...). You have constructors and destructors only for classes and structures.
However, the memory will be freed when the variable goes out of scope. This includes calling the destructor for classes and structures.
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
March 1st, 2004, 11:18 AM
#4
just a thought.
Is it possible to have a destructor for the defined types.
-
March 1st, 2004, 11:28 AM
#5
If by "defined" types you mean int, long, float, ... AFAIK, I don't think it's possible.
The closest you can do is create a class that will encapsulate just this type and create a constructor and destructor as suit you.
Code:
class MyInt
{
public:
MyInt() {i = 0;}
~MyInt() {i = 0;}
...
int i;
};
But I hardly see any interest in that ... At least about the destructor, even though the constructor can make sure we have an initialized value ...
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
March 2nd, 2004, 04:37 AM
#6
Ok thanks for the response..........
just to add..........
i have seen somewhere that we can define value as this also
int i(4)..........
is it correct.....
Thanks
Vishal
-
March 2nd, 2004, 04:49 AM
#7
Try it and see what your compiler says
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
March 2nd, 2004, 04:57 AM
#8
It means i=4
Hi Vishal,
You can define in that way also.
It means the same as i=4 (i.e. Calling Integer constructor)
The genaric data type are same as of user defined data type only.
For generic data type Operator Overloading (i.e +, - /) etc is done internally.So you can easily use that. The only difference with generic data type is you do not have access to that level.
Regards
Nagesh
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|