CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Confusion?

  1. #1
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    Confusion?

    if i declared something like this
    int myValue..........
    .
    .
    .
    .
    will this will call destructor when it goes out of scope?
    Thanks
    Vishal

  2. #2
    Join Date
    Jul 2001
    Location
    Pune-India
    Posts
    251
    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

  3. #3
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    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

  4. #4
    Join Date
    Dec 2003
    Posts
    99
    just a thought.
    Is it possible to have a destructor for the defined types.

  5. #5
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    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

  6. #6
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567
    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

  7. #7
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    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

  8. #8
    Join Date
    Jul 2001
    Location
    Pune-India
    Posts
    251

    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
  •  





Click Here to Expand Forum to Full Width

Featured