destructor

Usage of string::c_str on temporary string [duplicate]

我们两清 提交于 2019-12-01 04:00:29
This question already has an answer here: C++ destruction of temporary object in an expression 4 answers In regards to when temporary objects get destroyed, is this valid: FILE *f = fopen (std::string ("my_path").c_str (), "r"); Will the temporary be destroyed immediately after having evaluated the first argument to fopen or after the fopen call. Testing with the following code: #include <cstdio> using namespace std; struct A { ~A() { printf ("~A\n"); } const char *c_str () { return "c_str"; } }; void foo (const char *s) { printf ("%s\n", s); } int main () { foo (A().c_str()); printf ("after\n

Side effects when passing objects to function in C++

馋奶兔 提交于 2019-12-01 03:58:53
问题 I have read in C++ : The Complete Reference book the following Even though objects are passed to functions by means of the normal call-by-value parameter passing mechanism, which, in theory, protects and insulates the calling argument, it is still possible for a side effect to occur that may affect, or even damage, the object used as an argument. For example, if an object used as an argument allocates memory and frees that memory when it is destroyed, then its local copy inside the function

Does destroying and recreating an object make all pointers to this object invalid?

旧时模样 提交于 2019-12-01 03:49:14
This is a follow-up to this question . Suppose I have this code: class Class { public virtual method() { this->~Class(); new( this ) Class(); } }; Class* object = new Class(); object->method(); delete object; which is a simplified version of what this answer suggests. Now once a destructor is invoked from within method() the object lifetime ends and the pointer variable object in the calling code becomes invalid. Then the new object gets created at the same location. Does this make the pointer to the object in the calling valid again? This is explicitly approved in 3.8:7: 3.8 Object lifetime

Why is an overloaded delete not called when an exception is thrown in a destructor?

人盡茶涼 提交于 2019-12-01 03:25:52
I've written the below code which overloads the new and delete operators and throws an exception in the destructor. When the exception is thrown, why is the code in the delete operator not executed (and "bye" printed)? If it shouldn't be executed, (how) is the memory freed? Is one of the other delete operators called? Would overloading one of them instead result in the corresponding code being executed? Or is the memory simply not freed because a failed destruction implies that maybe it shouldn't be? #include <iostream> using namespace std; class A { public: A() { } ~A() noexcept(false) {

Explicit destructor in templated context

梦想的初衷 提交于 2019-12-01 03:17:25
I want to explicitly destroy a vector in a templated context. The following works for me (GNU C++ 4.3, 4.4 and Clang++ 1.1): template <typename T> void destroy_vector_owner(VectorOwner<T> *obj) { obj->v.~vector(); // further cleanup by Python API functions omitted } while it fails on Mac OS X v10.5's g++ ( i686-apple-darwin10-gcc-4.2.1 ) with expected class-name before ‘(’ token If I change it to obj->v.~vector<T>(); the code fails to compile with G++, but Clang can still handle it. Which is the correct idiom? Are any of these compilers known to be broken in this regard? Update : the

Pure virtual invocation from constructor and destructor

别说谁变了你拦得住时间么 提交于 2019-12-01 02:31:55
The C++ standard says that invoking a pure virtual function from a constructor or destructor is forbidden. What is the reason for this? Why should the standard place a restriction like this? At the point a class destructor is run, all subclass destructors have already been run. It would not be valid to call a virtual method defined by a subclass, for which its destructor has already run. A similar restriction exists around calling virtual methods in constructors. You can't call a virtual method for a subclass whose constructor has not yet run. It's the same reason you can't live in a house

Destructor - does it get called if the app crashes

China☆狼群 提交于 2019-12-01 02:29:38
Does a destructor get called if the app crashes? If it's an unhandled exception I'm guessing it does, but what about more serious errors, or something like a user killing the application process? And a few more potentially dumb questions: what happens to all the objects in an app when the app exits and all finalizers have been executed - do the objects get garbage collected or are they somehow all "unloaded" with the process or appdomain? is the garbage collector part of each application (runs in the same process) or is it independent? I would encourage you to try this for yourself. For

Usage of string::c_str on temporary string [duplicate]

Deadly 提交于 2019-12-01 01:54:54
问题 This question already has answers here : C++ destruction of temporary object in an expression (4 answers) Closed 5 years ago . In regards to when temporary objects get destroyed, is this valid: FILE *f = fopen (std::string ("my_path").c_str (), "r"); Will the temporary be destroyed immediately after having evaluated the first argument to fopen or after the fopen call. Testing with the following code: #include <cstdio> using namespace std; struct A { ~A() { printf ("~A\n"); } const char *c_str

Object destruction problem with MEF

旧城冷巷雨未停 提交于 2019-12-01 01:19:06
I use a static variable for holding the count of objects. In constructor I increase this variable. This way I know how many instances of the object are created. After using the objects, they are leaved dereferenced. I'm doubtful if MEF is holding references to these objects so I force GC to do a clean up (Using GC.Collect() method). I expect on next object creation this variable start from zero but it resumes from last number. I put a logging mechanism in destructor for tracing, and objects are destroyed only after the application is closed. Can I assume MEF has created other references to

How are the private destructors of static objects called? [duplicate]

拈花ヽ惹草 提交于 2019-12-01 00:59:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Cannot access private member in singleton class destructor I'm implementing a singleton as below. class A { public: static A& instance(); private: A(void) { cout << "In the constructor" << endl; } ~A(void) { cout << "In the destructor" << endl; } }; A& A::instance() { static A theMainInstance; return theMainInstance; } int main() { A& a = A::instance(); return 0; } The destructor is private . Will this get