4

I'm trying to call the destructor of a class explicitly in Turbo C++:

A a;
a.~A();

But it's showing an error:

Member identifier expected.

Is there any way to explicitly call a destructor in Turbo C++?

Shreyas
  • 1,927
  • 17
  • 34
  • 4
    It compiles fine on a compiler from this millenium http://coliru.stacked-crooked.com/a/c04b9139cc4e8794 (it has undefined behaviour, though, but that is a different matter) – R. Martinho Fernandes Sep 18 '13 at 11:08
  • 3
    Why do you want to call the destructor explicitly? – doctorlove Sep 18 '13 at 11:10
  • so is there any problem with compiler i'm currently using, it's turbo c++ – Shreyas Sep 18 '13 at 11:10
  • 1
    @ShreyasA: Yes, your compiler is too old. Just like nobody uses MS-DOS anymore, you should not use it. – Jesse Good Sep 18 '13 at 11:14
  • 2
    @ShreyasA Consider donating that compiler to some museum, they might be really happy to see one of those again ;) (Note: no offense intended. Try GCC or LLVM/Clang, those are the state-of-the-art compilers and they are free). – Daniel Frey Sep 18 '13 at 11:18
  • Destructor will be automatically called when the object goes out of scope (in your case, when the execution of the main function finishes, as `a` is a local variable in `main`). You don't to need to explicitly call it. – Rafi Kamal Sep 18 '13 at 11:09

5 Answers5

5

You don't need to call destructor explicitly, when an object with automatic duration goes out of scope, its destructor will be called implicitly.

void main()
{
    A a;
    //a.~A();  // you need to comment out this line
} // a.~A() is called again, you try to destroy an object twice.
billz
  • 44,644
  • 9
  • 83
  • 100
  • 1
    true, but not answering the question. – Walter Sep 18 '13 at 11:13
  • 1
    @Walter what did I miss? – billz Sep 18 '13 at 11:14
  • yeah that's correct but take a look at this http://stackoverflow.com/questions/11884168/c-destructor-being-called-twice?rq=1 – Shreyas Sep 18 '13 at 11:15
  • I just realise that there is no question ... Should close it. Anyway, I understood the question to be *why did my attempt fail to call the destructor explicitly* (which in this particular example makes no sense, but may be useful otherwise). – Walter Sep 18 '13 at 11:16
  • 1
    I think this is a good answer. The question seems like an XY question. – Simple Sep 18 '13 at 11:16
5

From this link, it seems you actually can do it, the valid format is:

A a;
// a.~A(); // Wrong - Member identifier expected
a.A::~A(); // Valid

But I don't have a Turbo C++ compiler handy to test it, so you'll have to test it.

[Edit]

OP tested it, it works.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
3

While @billz is right that you don't have to call it explicitly, the line itself should be valid. A destructor is a regular method and can be called like that. This can be useful if you implement your own operator new and operator delete but in your case, just stick with billz answer.

The problem which leads to an error message seems to be that you have a too-old compiler, as evidenced by <iostream.h> or void main(). Fixing the old-school-stuff (but not the a.~A();) the code compiles with GCC 4.8.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • yeah i think thats the reason because http://coliru.stacked-crooked.com/a/c04b9139cc4e8794 is running perfectly – Shreyas Sep 18 '13 at 11:19
  • I think a.~A(); causes virtual destructor call and a.A::~A(); causes nonvirtual destructor call. – Johny Sep 18 '13 at 11:23
  • @Johny Only if `~A` is virtual which in the OPs example it is not. And if it is virtual, you are right but I also fail to see how this is relevant here. – Daniel Frey Sep 18 '13 at 11:25
  • There's nothing new about `a.~A()`. This worked in compilers I was using as early as 1991. (IIRC, g++ had a bug which meant that it didn't work with it. But that bug has been fixed ages ago.) – James Kanze Sep 18 '13 at 12:43
  • @JamesKanze I wasn't implying that it's new, I was only saying that bugs in such a basic area are more likely to be caused by old compilers. An someone else already commented, compilers from this millenea don't have problems with that. :) – Daniel Frey Sep 18 '13 at 12:45
  • You will _not_ use a an explicit destructor if you implement your own `operator new` and `operator delete`. – James Kanze Sep 18 '13 at 12:55
1

As per your requirement, following code works well

 #include <iostream>

 class A
 {
    public:
     A() { }
     ~A() { std::cout<<"\n ~A()"; }
 };

 int main()
 {

      A a;
      a.A::~A();
      return 0;
  }

But beware of Calling the D'tor explicitly. Because compiler will insert the code for an auto obejcts that are inside the function. In the above code will result in calling the D'tor twice i.e

 int main()
 {
   A a;
   a.A::~(); //which we provided
   a.A::~(); // provided by the compiler for auto objects
   return 0;
 }

We have to explicitly call the D'tor only for the placment new operator here for more details.

Saravanan
  • 1,270
  • 1
  • 8
  • 15
0

If the compiler doesn't allow the standard syntax for an explicit destructor call, then it probably doesn't allow you to do that.

Your compiler is about twenty years old, and the language has been through at least two major revisions since then. I don't have enough historical knowledge (or interest) to know whether its dialect is supposed to allow explicit destruction; but if you want to write modern(ish) C++ then you'll need a modern(ish) compiler.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Explicit destruction has been in the language at least since CFront 2.1 (late 1980's). IIRC, g++ had a bug which meant that you had to write `a.A::~A()`, but that would be version 1.49 or 2.1.2; it certainly worked in 2.95.2 (or something like that). We're at something like 4.8 now, so that means that it has been fixed for a long, long time. – James Kanze Sep 18 '13 at 12:46
  • @JamesKanze: Thanks for the historical context; so it seems the problem is with whichever dialect this ancient compiler impelements, not the "official" language of the time. But the main point of my answer remains: use a modern(ish) compiler for a modern(ish) language, if you want help from modern(ish) programmers. – Mike Seymour Sep 18 '13 at 14:17
  • Yes. I'm not a fan of using the very latest features of the newest compiler (in general), but this feature was introduced with user overloading of `operator new` and `operator delete`, about 15 years ago. (And in our field, 15 years is an eternity.) – James Kanze Sep 18 '13 at 14:36