is it uncommon to explicitly call the destructor of an object.

This is a discussion on is it uncommon to explicitly call the destructor of an object. within the C++ Programming forums, part of the General Programming Boards category; While I'm sure it's not significant, what if you're working with a temporary string to append it to a larger ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    is it uncommon to explicitly call the destructor of an object.

    While I'm sure it's not significant, what if you're working with a temporary string to append it to a larger string, when you're done with the temp, would it be pretty useless to explicitly call the destructor or is it just uncommon. Or any other objects that are made to be temporary.

    Or should I just dynamically allocate?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll probably use the temporary in a small scope. At the end of the scope the string destruction will be called automatically for you, and you can reuse the memory.

    If you really are short of memory, you can create a smaller scope just by putting the desired lifetime of the string between a pair of curly brackets

    Code:
    //some code
    { //just a new scope
    std::string temp(1000000, 0); //large temporary string
    //some more code
    } //end of scope, temp is gone

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You shouldn't explicitly call a destructor. See: http://www.parashift.com/c++-faq-lit....html#faq-11.5 (if you read far enough, it notes one exception to this rule)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    You can explicitly call your own destructor, if you want your destructor to print something like:
    Code:
    destructor called
    as an example...

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you run this program from command prompt, you'll see that the destructor gets called twice. This doesn't look good, if it were to do something important. (You'll probably need special precautions when coding the destructor, making it more complicated than it needs to be.)

    Code:
    #include <iostream>
    
    class A
    {
        public:
            A() {std::cout << "constuctor called" << std::endl;}
            ~A() {std::cout << "destructor called" << std::endl;}
    };
    
    int main()
    {
        A a;
        a.~A();
    }
    This is simpler to use and you can still see "destructor called" before std::cin.get()
    Code:
    #include <iostream>
    
    class A
    {
        public:
            A() {std::cout << "constuctor called" << std::endl;}
            ~A() {std::cout << "destructor called" << std::endl;}
    };
    
    int main()
    {
        {
            A a;
        }
        std::cin.get();
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> You can explicitly call your own destructor, if you want your destructor to print something like

    Even if you want your destructor to print something, there is still no reason to call it explicitly. Read the link.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I was originally thinking along the lines of the temporary scope rather than calling it explicitly, just wondering, thanks for the links.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM
  4. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21