const objects calling destructor

Posted on 2006-03-29
Last Modified: 2008-03-04
    i have come across a strange problem, causing conflict in concepts.

    It is known that a const object can only call const functions with the exception of constructors and destructors which are called only once and automatically. But i experienced that we can also call the destructor explicitly (c++) :?, but that violates the const object concept as we can call destuctor with it and can do any thing in the function.

   On net the only reason i found for allowing explicit destructor call is to clear the memory, but we can write some memory clean function, as we do initialization functions corresponding to initialization constuctors !!! i am not satisfied with this argument.

  so my question is why explicit call of destructor is allowed? and how is justifies the const obj concept?

  hope i can get some better reasoning from here. really looking forward for replies.

Question by:fatimao
    LVL 12

    Accepted Solution

    LVL 15

    Assisted Solution

    As the links provided by rajeev_devin explain, the explicit destructor call is allowed as the counterpart of placement new, to make it possible to destroy an object without necessarily releasing its storage.  It's true that one could write a different function to do this, but then if the object were to be used without placement new, there would be two functions that do the same thing, which would be undesirable.  Since the concept of a destructor function that destroys an object was already defined, it was logically consistent to use the same function to do the same job in the placement new case.

    The designers of the language had two choices about attempts to call the destructor of a constant object:  they could allow it or not.  If they didn't allow it, all constant objects could never be destroyed and would hang around indefinitely.  If they did allow it, a sufficiently deranged programmer could write code that would destroy a constant object.  They decided that the second case was the lesser evil.  Requiring all constant objects to have indefinite lifetimes would be both inconvenient and inefficient.  For that matter, to be consistent, you would also have to forbid running the constructors of constant objects, because they too change object content, so you could never create a constant object in the first place.  So on the whole, it is most practical to exclude construction and destruction from the class of operations that are not allowed on constant objects.
    LVL 3

    Expert Comment

    Do not think of constructors & distructors as an ordinary member functions. These are special member functions, one initializes and other deinitializes.
    Any object can call distructor explicitly, irrespective to whether it is a const or a non const.
    I do not find any strange thing in const object calling distructor explicitly.
    If you have a problem with,
    1. const obj calling distructor, let me tell you a basic thing any thing created has to be destroyed so distructor call is a must.
    2. const obj calling distructor explicitly, here objects are allowed for this.

    All the best
    Prashant Sabnekar

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now