Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ object destroy

Tags:

c++

I'm moving from java to c++, and i'm try to understand constructing/destructing objects. In java when i do

Myclass c=createANewObject();
c=createANewObject();

the old c is garbage collected, and another one is created with the same name.

if i try to do the same in c++ i get strange behaviour.

class my_class 
{
string content;
time_t t;
public: 
my_class(string c);
~my_class();
};

my_class::my_class (string c) 
{
content=c;
cout<<"Init -" << content << "-" << t <<endl;
}

my_class::~my_class()
{
cout<<"Destroyed -" << content << "-" << t <<endl;

}

my_class get_new_object(string s)
{
   my_class c(s);
   return   c;
}

int main()
{
    my_class c=get_new_object("A");
    c=get_new_object("B");
}

Instead of getting

Init -A-
Init -B-
destr.A
destr.B

because first i create A, then create B, then A is destroyed, and the scope is ended so B is destroyed

I get

Init -A-1456178128 Init -B-1456178131 Destr.B-1456178131 Destr.B-1456178131

So my A is created and not destroyed, and B... is destroyed two times?

like image 997
Ciccio Pasticcio Avatar asked Feb 22 '16 21:02

Ciccio Pasticcio


People also ask

How do you destroy an object in CPP?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .

Can you destroy an object in C#?

Unfortunately, there is no such thing as destroying an object in C#. We can only dispose of an object in C#, which is only possible if the class implements IDisposable . For the objects of any other class types, we have to assign a null value to the class object.

What is the function of destroy?

The destroy() function performs any processing necessary before the database server removes a row that contains opaque data. The database server calls the destroy() function just before it removes the internal representation of an opaque type from disk.

How do you release an object in Objective C?

The system that Objective-C uses is called retain/release. The basic premise behind the system is that if you want to hold on to a reference to another object, you need to issue a retain on that object. When you no longer have a use for it, you release it. Similar to Java, each object has a retain count.


2 Answers

In Java your code does the following sequence:

  • Make a new object
  • Set the reference c to refer to that object
  • Make another new object
  • Release the reference c from the old object and make it refer to the new object
  • The old object now has no references and will later be garbage collected

In C++ your code is quite different. Don't be fooled by the similar-looking syntax. In C++ you can perform almost that same steps as your Java code did; by using different syntax. But the syntax you did actually use does the following:

  • Make an object get_new_object::c("A")
  • Return a copy of that object
  • Destroy the get_new_object::c
  • Make object main::c initialized by copying the returned copy
  • Destroy the returned copy
  • Make an object get_new_object::c("B")
  • Return a copy of that object
  • Destroy the get_new_object::c
  • Update main::c by copying detail from the returned object
  • Destroy the returned object
  • (At the end of main) Destroy main::c

Some of the above copies may be optimized out by a process called copy elision. If you use a compiler switch to disable copy elision you should see all of the above steps, i.e. 5 destructors, 2 normal constructors, and (if you also add output for the other special functions), 3 copy-constructors and 1 assignment operator.

NB. In C++11 the temporary objects may be moved in and out of (if the compiler decides not to use elision) rather than copied. But I leave this out to keep the list simple.

like image 146
M.M Avatar answered Sep 18 '22 19:09

M.M


    my_class get_new_object(string s)
    {
    my_class c(s);
    return  c;
    }

    int main()
    {
    // Sequence of events:
    //    - get_new_object() called
    //    - Inside get_new_object() "my_class c() instance created on stack and constructor called --> This is the first constructor call you see
    //    - Inside get_new_object() "return c;" statement  first creates a copy of 'c' and assigns to the my_class c in main() --> This is your second constructor call
   //     - return c; initiates destruction of the my_class c which was created inside get_new_object() since it's on the stack and the function is going out of scope  --> This is your first Destructor call
   //     - main ends --> This is when the my_class c of main() goes out of scope and the destructor is called again.
    my_class c=get_new_object("A");
    c=get_new_object("B");
    }
like image 41
Sid Avatar answered Sep 22 '22 19:09

Sid