calling a destructor

Posted on 2004-11-06
Medium Priority
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
  • 2
LVL 19

Accepted Solution

mrwad99 earned 2000 total points
ID: 12514379
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

ID: 12514421
For more details:


Answers 4, 5, and 10.
LVL 19

Expert Comment

ID: 12514474
>> 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

840 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