-2

I have a class called City the objects of which depend on 3 variables: string name, int area, int population and a constructor for this. I have a destructor which looks like this City::~City() {}

In main.cpp I'm creating an object like this: City city1 = City("New York", 123, 100000)

After that I want to delete the object city1 that I created, I'm trying:

  1. city1.~City(); - nothing happens

  2. delete city1; or delete[] city1; - Cannon delete expression of type 'City'

  3. delete [city1]; - Expected body of lambda expression

What should I do to delete class object? Should I change the destructor somehow?

primadonna
  • 142
  • 4
  • 12
  • Please, post a [mcve]. How to delete an instance very depends on how it was constructed. Btw. every reasonable C++ book should mention this topic... ;-) – Scheff's Cat Oct 12 '20 at 13:38
  • 2
    You never aquired an object with `new`, hence nothing is there to `delete`. – πάντα ῥεῖ Oct 12 '20 at 13:38
  • Just saw the `City city1 = City("New York", 123, 100000)` on the 2nd glance. In this case, _you_ don't delete it. It's auto-deleted when you leave the scope. – Scheff's Cat Oct 12 '20 at 13:39
  • 1
    You shouldn't try to delete it at all - the object will be destroyed automatically. You should probably invest in a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 12 '20 at 13:40

2 Answers2

3

If you declare a City object like this:

{
  City city1 = City("New York", 123, 100000);
  // ...

then the correct code to delete this object is:

}

Since c++ has deterministic destruction of objects with automatic storage duration (as is the case for city1), it will be correctly cleaned up as soon it goes out of scope.

In general, this requires the destructor of City to be written correctly, but in your case, since it only contains a string and 2 ints, the implicit destructor will do the right thing.

cigien
  • 57,834
  • 11
  • 73
  • 112
0

What should I do to delete class object?

Depends on how you create the instance. Objects in automatic, static and thread local storage as well as temporary objects are destroyed automatically. All variables have one of these storage classes.

Dynamic objects must be destroyed explicitly. Typically the ideal way to delete dynamically allocated objects is transfer the ownership to a smart pointer that deletes the pointed object when the smart pointer is destroyed.

City city1 = City("New York", 123, 100000)

This is a variable. As such, the object is destroyed automatically.

Should I change the destructor somehow?

Depends on what responsibilities the class has. Often, the ideal design for a class is one where user defined destructor is not needed.


I'm trying:

  • city1.~City(); - nothing happens

That is most likely wrong in your case. If you don't create another instance in place of the destroyed one, then the behaviour of the program will be undefined when the variable is destroyed.

delete city1; or delete[] city1; - Cannon delete expression of type 'City'

city1 is not a pointer, so it is not something that can be deleted.

delete [city1]; - Expected body of lambda expression

There is no syntax like this in C++.

eerorika
  • 232,697
  • 12
  • 197
  • 326