0

Would it be possible through funny initialization and/or casting to base classes to achieve an instance of an abstract class or class which has pure virtual methods?

I'm using the VS14 compiler and I'm wondering if the compiler could miss this? The class and its use are in separate assemblies.

To be clear I'm not trying to achieve this, just looking at bugs and wondering if this could lead to a runtime exception of calling a pure virtual method.

Ivajlo Donev
  • 195
  • 2
  • 10
  • 1
    ...Are you sure you're not dealing with a [slicing problem](http://stackoverflow.com/questions/274626/what-is-object-slicing)? It *seems* related... – jaggedSpire Nov 01 '16 at 21:11
  • @jaggedSpire You can't slice into an abstract class since you can't instantiate an abstract class anyway. – Brian Bi Nov 01 '16 at 21:12
  • If you happen to see a call to a pure virtual function, that's because the function is called (directly or indirectly) from the constructor or destructor of the abstract class. – Eran Nov 01 '16 at 21:16
  • 1
    @Brian what is [this](http://coliru.stacked-crooked.com/a/ed169121e676db41), if not object slicing with an abstract base class? – jaggedSpire Nov 01 '16 at 21:23
  • unfortunately I don't think my problem is calling methods from the constructor/destructor since the problem call is of the form pInstance->method(); I did find an GCC issue http://stackoverflow.com/questions/27675893/gcc-bug-implicit-conversion-to-a-derived-class and wondered if there might be some other well known visual studio type bugs – Ivajlo Donev Nov 01 '16 at 21:24
  • @jaggedSpire Thanks for the suggestion, its not something I considered could happen but its perfectly possible that something like this happening at some point (the code i have to work with is a bit of a mess) – Ivajlo Donev Nov 01 '16 at 21:33
  • @jaggedSpire Sorry, what I meant is that slicing can't create an object of abstract base class type. Of course it can modify that subobject of an existing object. – Brian Bi Nov 01 '16 at 21:41
  • @Brian ah, yeah. I guess I was probably reading more into OP's unexpected abstract class behavior and you more into the mention of instantiating abstract classes? – jaggedSpire Nov 01 '16 at 21:44
  • @jaggedSpire Can you reproduce an error of the sort OP is experiencing using slicing? – Brian Bi Nov 01 '16 at 21:45
  • @Brian Can't think of one off the top of my head, and it looks like doing so will be rather difficult without resorting to dirty tricks... – jaggedSpire Nov 01 '16 at 22:04

1 Answers1

0

If your program contains undefined behaviour, then anything can happen.

But in general, no, unusual casting will not allow you to create a complete object of abstract base class type.

That error can, however, be caused by trying to call the pure virtual method during construction or destruction (which is a form of UB), at which point the complete object does not exist, so the appropriate final overrider is not called. To avoid such issues, it is usually best to not call virtual methods (directly or indirectly) for the object under construction or destruction.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312