G
grahamo
Hi,
I recently did a test which required the user to note that a base
class had a non virtual destructor. Hence the dtor wouldn't be called
when the object was deleted.
The code is below (from memory) it's straightforward enough. My
question is this;
Does the spec say that the base classes dtor *wont* be called, or that
the behavior is undefined....The difference is subtle but all the same
its there. Am I correct in thinking that the behaviour is undefined?
According to Scott Meyers, it's undefined. I don't have a copy of the
spec but I'd like to get clarification.
thanks mulcho.
GrahamO
#include <iostream>
using namespace std;
class base
{
public:
base()
{
cout << "base()" << endl;
}
~base()
{
cout << "~base() " << endl;
}
};
class derived : public base
{
public:
derived()
{
cout << "derived()" << endl;
}
~derived()
{
cout << "~derived() " << endl;
}
};
derived* makeDerived()
{
return new derived();
};
base* makeBase()
{
return new base();
};
void cleanUp(base* b)
{
delete b;
}
int main(int argc, char** argv)
{
base* b = makeBase();
derived* d = makeDerived();
cleanUp(b);
cleanUp(d);
return 0;
}
I recently did a test which required the user to note that a base
class had a non virtual destructor. Hence the dtor wouldn't be called
when the object was deleted.
The code is below (from memory) it's straightforward enough. My
question is this;
Does the spec say that the base classes dtor *wont* be called, or that
the behavior is undefined....The difference is subtle but all the same
its there. Am I correct in thinking that the behaviour is undefined?
According to Scott Meyers, it's undefined. I don't have a copy of the
spec but I'd like to get clarification.
thanks mulcho.
GrahamO
#include <iostream>
using namespace std;
class base
{
public:
base()
{
cout << "base()" << endl;
}
~base()
{
cout << "~base() " << endl;
}
};
class derived : public base
{
public:
derived()
{
cout << "derived()" << endl;
}
~derived()
{
cout << "~derived() " << endl;
}
};
derived* makeDerived()
{
return new derived();
};
base* makeBase()
{
return new base();
};
void cleanUp(base* b)
{
delete b;
}
int main(int argc, char** argv)
{
base* b = makeBase();
derived* d = makeDerived();
cleanUp(b);
cleanUp(d);
return 0;
}