Is calling non-member function from destructor OK? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In C++, is it OK to call a non-member function (either a free function or a member of other objects) from within the destructor? I am calling a (non-virtual, although in this case it shouldn't really matter) method of an object of a different class. This method crashes on trying to access its members.
On the other hand, if this different object is a child of the destructed object (Qt), does this matter?

In general a destructor can call any function it needs to properly destroy the object. However, there are a couple of caveats:
If the function called from a destructor throws an exception, the exception must be caught and handled in the destructor.
The function called from a destructor must not unconditionally create and destroy objects of the type to which the destructor belongs (since this will result in infinite recursion).

Related

dynamic allocation of class object without constructor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I dynamically allocate a class object that has no defined constructor?
I tried:
A * newPtr = new A();
But it's giving me some kind of memory leak on gdb.
Cheers!
If there's no defined constructor and standard rules allow it then you get a implicitly-declared default constructor, otherwise your code wouldn't compile at all.
So if you are not defining any constructor and your code compiles then for sure a default one is declared and defined (so you actually have a constructor).
You get a leak because you need to delete the pointer to free its memory from the heap.

Declaring instance without variable name c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
After asking C++ Error linking in consumer file caused by static data field, I tried two different declarations for an instance of StateConservator:
StateConservator cs(*pContainer, pDoc->GetConfiguration());
and
StateConservator(*pContainer, pDoc->GetConfiguration());
The first did what I want, it only passes on the destructor only after the end of the scope. The second passes on the destructor in the own line of the declaration itself.
Is the compiler behaving correctly? If it is the correct behavior what is the way to declare an anonymous variable in that line?
You cannot have "unnamed" objects in C++. In the second case, the object is created and destroyed instantaneously because it is not associated with a name. The association with a name (e.g., variable) gives an object scope which controls its lifetime. By not naming an object, it's lifetime is bound to the statement. If it is given a name, then it is bound to the scope that the name is declared in.

C++: why isn't the destructor designed like delete of a pointer? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If we delete a pointer for the first time, it release the memory and assign NULL to the pointer.
If we delete the pointer (with NULL value) for the second time, nothing happens, and no error throws out.
Then why isn't the destructor designed like delete of a pointer,
We manuall call destructor of an object, and assign something to the object, like NULL.
so that destructor can be called for many times without error?
[Update] I meant we assign NULL explicitly to the pointer.
The whole purpose of constructors and destructors is to avoid manual calling of the destructor. It's designed so that objects are automatically destroyed when no longer in use. This makes it harder for the programmer to accidentally forget to delete an object; or to use an object that has already been deleted.

Understanding Constructors in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't understand the purpose of constructors and how they function in C++. I understand that a constructor a is a function within a class that has the same name as the class but does not have a return type.
The purpose of a constructor in C++ is to reliably initialize raw memory, turning it into a useful object by establishing the chosen class invariant (what you can always assume about the object between calls of its member functions).
In contrast, an assignment has to change the value of an already initialized object, and may for example have to deallocate buffers that already have been established.
As long as you don't use very low level features of the language there is effectively a constructor call guarantee: that every object instantiated from a type T that has at least one user defined constructor, gets a single external call to a T constructor, which happens before anything else. Conversely, when you call a constructor by using the type name as a pseudo function name, T(), or with arguments, a T object is created. So the guarantee works both ways, and means that object creation involves constructor call and vice versa.

What is the convention about class method return this pointer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Or in a class method, call other class' method/free function and pass this pointer as the parameter. Is there any good practice for this? Or it is a bad practice and I should not doing so?
It usually makes no sense to return the this pointer from class meber functions.
(since the caller must be already holding a reference to the instance,, to call the method/operator in question, and you cannot change it returning di9fferent values anyway).
It makes sense to return *this from various types of member functions (specific operators in particular, e.g. operator=()), to enable chaining of operations on results.
NOTE:
Some of the overloadable operator signatures (as the mentioned assignment operator) require to implement such behavior to work correctly with the semantics defined by C++!