Possible Duplicate:
Is it worth setting pointers to NULL in a destructor?
Do I have to do something like so:
~MyClass()
{
delete[] my_data_;
my_data_ = nullptr;//DO I HAVE TO WRITE THIS LINE?
}
Thank you.
...
Suppose I have a class like this:
#include <iostream>
using namespace std;
class Boda {
private:
char *ptr;
public:
Boda() {
ptr = new char [20];
}
~Boda() {
cout << "calling ~Boda\n";
delete [] ptr;
}
void ouch() {
throw 99;
...
I have a python object which wraps a sensitive and important resource on the system. I have a cleanup() function which safely releases various locks used by the object.
I want to make sure that after a call to cleanup() the object becomes unusable. Ideally, any call to any member function of the object would raises an exception. Is the...
This has probably been asked before on SO, but I were unable to find a similar question.
Consider the following class hierarchy:
class BritneySpears
{
public:
virtual ~BritneySpears();
};
class Daughter1 : public BritneySpears
{
public:
virtual ~Daughter1(); // Virtual specifier
};
class Daughter2 : public BritneySpears...
I know that my destructors are called on normal unwind of stack and when exceptions are thrown, but not when exit() is called.
Are there any other cases where my destructors are not going to get called? What about signals such as SIGINT or SIGSEGV? I presume that for SIGSEGV, they are not called, but for SIGNINT they are, how do I kno...
Consider the following code:
void foo()
{
{
CSomeClass bar;
// Some code here...
goto label;
// and here...
}
label:
// and here...
}
Will the destructor of bar be called ?
...
I have problems understanding why when I create two or more nodes (as shown below), the function void del_end()will only delete the char name[20] and not the whole node . How do I fix this problem without memory leak?
#include <iostream>
using namespace std;
struct node
{
char name[20];
char profession[20];
int age;
no...
I guess I don't fully understand how destructors work in C++. Here is the sample program I wrote to recreate the issue:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
struct Odp
{
int id;
Odp(int id)
{
this->id = id;
}
~Odp()
{
cout << "Destructing Odp " << id << ...
The C++ Standard states the following about virtual functions that have exception specifications:
If a virtual function has an exception-specification, all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the exception-spe...
I have a class that's similar to the following:
class Person
{
private static $_sqlData;
public function __construct($id)
{
if (!self::$_sqlData)
{
self::$_sqlData = // GET THE DB STUFF
}
}
public function getName()
{
return self::$_sqlData['name'];
}
}
This has bee...
A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed?
It will be great to know what exac...
In my opinion, the following code (from some C++ question) should lead to UB, but the it seems it is not. Here is the code:
#include <iostream>
using namespace std;
class some{ public: ~some() { cout<<"some's destructor"<<endl; } };
int main() { some s; s.~some(); }
and the answer is:
some's destructor
some's destructor
I learned f...
Does anyone know any trick I could use to keep the Derived class until the base class destructor have been called?
i.e:
#include <iostream.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
virtual ~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of ...
In OpenGL, one often writes code like this:
glPushMatrix();
// modify the current matrix and use it
glPopMatrix();
Essentially, the state is changed, then some actions are performed that use the new state, and finally the state is restored.
Now there are two problems here:
It's easy to forget to restore the state.
If the code in be...
If a window is closed (like with sending WM_CLOSE), are the destructors of objects called?
I followed my source code with a break point in that situation, but the compiler doesn't seem to pass through my destructor.
Is the program closed without calling any destructors?
...
I have an object on the stack for which I wish its destructor to skip some work when the destructor is being called because the stack is being unwound due to a specific exception being thrown through the scope of the object on the stack.
Now I could add a try catch block inside the scope of the stack item and catch the exception in ques...
Is a class destructor in PHP predictable? When is the destructor called?
Like in many languages, will a class destructor be called as soon as the object goes out of scope?
...
I have
struct IMyInterface
{
virtual method1() = 0;
virtual method2() = 0;
};
GCC insists that I have
struct IMyInterface
{
virtual method1() = 0;
virtual method2() = 0;
virtual ~IMyInterface(){};
};
I dont see why. A pure interface is all about the interface (duh). The destructor is part of the internal implementati...
I have some code that is crashing in a large system.
However, the code essentially boils down to the following pseudo-code.
I've removed much of the detail, as I have tried to boil this down to the bare bones;
I don't think this misses anything crucial though.
// in a DLL:
#ifdef _DLL
#define DLLEXP __declspec(dllexport)
#else
#define ...
Hi, as I've asked in here and after a while I've agreed and accepted right answer to that question I was just thinking, if would it be useful to have something like "moving destructor" which would be invoked on moved object everytime we used move ctor or operator=.
In this way we would have to specify only in move dtor what we want from...