Error: C++ base class needs at least one virtual function

Ethan gooberman at gmail.com
Sat Jul 27 19:58:05 UTC 2019


I know how this is going to get in to a discussion about how 
virtual destructors are necessary, but in this case it's not and 
I know better than the compiler. tl;dr is "How do I make the 
compiler behave the way I want it to?"

This is going to be representative of code in my runtime. Every 
object I deal with will be a reference, so classes are a logical 
choice. They'll be stored in an immutable hashmap using a unique 
ID, they have type checking built in to the ID, and my reference 
retrieval object will do the type checking and casting.

And the only reason I'm using classes? Because struct inheritance 
is still considered a monumentally bad thing despite the solid 
use cases. I know the outright hacks around it, my DConf 2016 and 
2017 talks went in to detail about it. But really, I don't want 
this code to employ such gross hacks for what should be something 
simple.

Notice I even enforce every method to be final in my derived 
class to emphasise that *I do not want nor need virtuals ever in 
this code*.

--------

extern(C++):

class Base
{
     int uniqueID;
}

class Derived : Base
{
final:
     @property UniqueID( int val ) { uniqueID= val; return 
uniqueID; }
     @property UniqueID() const { return uniqueID; }
}

int main( string[] args )
{
     Base obj = new Base;
     obj.Value = 42;

     import std.stdio : writeln;
     writeln( obj.Value );

     return 0;
}


More information about the Digitalmars-d mailing list