How to manually delete an instance of a class?

How to manually delete an instance of a class?

Example:

#include <iostream>
#include <cstring>

class Cheese {
private:
    string brand;
    float cost;
public:
    Cheese(); // Default constructor
    Cheese(string brand, float cost); // Parametrized constructor
    Cheese(const Cheese & rhs); // Copy construtor
    ~Cheese(); // Destructor
    // etc... other useful stuff follows
}

int main() {
    Cheese cheddar("Cabot Clothbound", 8.99);
    Cheese swiss("Jarlsberg", 4.99);

    whack swiss; 
    // fairly certain that "whack" is not a keyword,
    // but I am trying to make a point. Trash this instance!

    Cheese swiss("Gruyère",5.99);
    // re-instantiate swiss

    cout << "\n\n";
    return 0;
}
+4
source share
5 answers

Without knowing the use case or the actual problem you want to solve (please read the XY problem , your question is a good example of this) the simplest way is to simply reassign:

Cheese swiss("Jarlsberg", 4.99);
...
swiss = Cheese("Gruyère",5.99);

This may require you to execute the assignment operator, but according to the rules of three or five options, you still have to do this (but the assignment operator is not needed if you follow the zero rule ).

, swiss:

Cheese* swiss = new Cheese("Jarlsberg", 4.99);
...
delete swiss;
swiss = new Cheese("Gruyère",5.99);

- , , ++. ( ) , . , , , , , .

, , , , , :

Cheese swiss("Jarlsberg", 4.99);
...
{
    Cheese swiss("Gruyère",5.99);
    // In here the swiss cheese is a Gruyère
    ...
}
// Out here the swiss cheese is a Jarlsberg

, , , , , . , () , , jarlsberg gruyere, gruyere , , "".

+8

, .

Cheese swiss("Toe", 3.14)

{
    Cheese swiss("Ear", 15.9);
}

, , .

, .

  Cheese *swiss = new Cheese("toe", 3);

   // do something with swiss.

   delete swiss;    // throw it away.

   swiss = new Cheese("Ear", 7);

   // do something with swiss.

   delete swiss;    // throw it away.

.

+4

, . , , - .

, , , , , .

typename std::aligned_union<0, FirstType, RestTypes...>::type m_buffer;

:

new (&m_buffer) AssignType(forward<T>(x));

:

(HeldType*)(&m_buffer)->~HeldType();

, . , dtors. , . , delete . , , - , , , , .

+2

++ , . , - , . , - . , "" , , , . ?

.

Community Wiki !

#include <iostream>
#include <string>
#include <new>

template <typename T, typename ...As>
inline void Reconstruct(T &ojt, const As&... ctor_args) {
    // Explicitly call the destructor, to destroy the object
    // without doing anything to its memory allocation.
    ojt.~T();

    // Use Placement new to call a constructor.
    // Instead of allocating memory somewhere for a new object,
    // this specifies where the new object will be constructed --
    // given here as the location of the object previous
    // incarnation.
    // Also pass any arguments to the constructor.  I forced
    // these arguments to be const references in order to
    // avoid extra copying, so "move" construction won't work
    // and steps might need to be taken to accommodate other,
    // more unusual constructors.
    new(&ojt) T(ctor_args...);
}

class Cheese {
    std::string brand;
    float       cost;
public:
    Cheese() : cost(0) {}
    Cheese(std::string brand, float cost) : brand(brand), cost(cost) {}
    Cheese(const Cheese & rhs) = default;
    ~Cheese() = default;
    friend std::ostream& operator << (std::ostream& os, const Cheese& chz) {
        return os << "[brand:\"" << chz.brand << "\" cost:" << chz.cost << ']';
    }
};

int main() {
    Cheese cheese, fromage;
    std::cout << "cheese = " << cheese << '\n';

    Reconstruct(cheese, "Cabot Clothbound", 8.99f);
    std::cout << "cheese = " << cheese << '\n';

    Reconstruct(fromage, cheese);
    std::cout << "fromage = " << fromage << '\n';

    Reconstruct(cheese, "Jarlsberg", 4.99f);
    std::cout << "cheese = " << cheese << '\n';
}
0

- , optional.

std::experimental::optional<Cheese> swiss(std::experimental::in_place, "Jarlsberg", 4.99);

swiss = std::experimental::nullopt; // Calls Cheese::~Cheese internally

// re-instantiate swiss
swiss.emplace("Gruyère",5.99);

As long as you don't save the option, you can probably rely on a compiler to optimize the extra internal bool.

0
source

All Articles