Under what circumstances will _declspec( novtable ) cause an access violation?
For example, this code doesn’t:
class __declspec(novtable) Base
{
public:
virtual ~Base() { };
virtual int Foo() const = 0;
virtual int Bar() const { return 2; };
};
class A : public Base
{
public:
int Foo() const { return 1; };
};
int main(int argc, char* argv[])
{
A a;
volatile int a1 = a.Foo();
volatile int a2 = a.Bar();
Base* c = new A();
volatile int c1 = c->Foo();
volatile int c2 = c->Bar();
delete c;
return 0;
}
This code also does not:
class __declspec(novtable) Base
{
public:
virtual ~Base() { };
};
int main(int argc, char* argv[])
{
Base a;
}
But, this code will:
int main(int argc, char* argv[])
{
Base* a = new Base();
delete a; // access violation
}
Why does the code in the first two example not throw on the destructors?
The code where you get an access violation is the code where you’re explicitly calling “delete” on the novtable class.