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?
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() .
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.
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.
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.
In Java your code does the following sequence:
c
to refer to that objectc
from the old object and make it refer to the new objectIn 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:
get_new_object::c("A")
get_new_object::c
main::c
initialized by copying the returned copyget_new_object::c("B")
get_new_object::c
main::c
by copying detail from the returned objectmain::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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With