calling a destructor

Posted on 2004-11-06
Last Modified: 2010-04-01
can I call the destructor of a class?  I am writing a dll in C++ for a client application and I need to dissconnect somehow.  Can/Should I just call the TCPClient's destructor?
Question by:axnst2
    LVL 19

    Accepted Solution

    Why would you want to ?  You should let the objects destroy themselves naturally when they go out of scopt, unless theyare allocated on the heap via 'new' in which case you delete them via 'delete'

    You can explicitly call a destructor via


    but if the object later goes out of scope naturally you will have memory access errors when the compiler tries to clean up a second time.
    LVL 15

    Expert Comment

    For more details:

    Answers 4, 5, and 10.
    LVL 19

    Expert Comment

    >> You can explicitly call a destructor via

    >> ClassName::~ClassName()

    Oops of course that should be

    objectInstance(. or ->)~<ClassName>();

    and more accurately memory access errors if the class constructor explicitly allocated memory via new.

    Nice link from efn too :)

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    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.

    745 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

    19 Experts available now in Live!

    Get 1:1 Help Now