Skip to content

GDCLASS macro prohibits use of pure virtual functions #1287

Description

@Naros

Godot version

4.2 beta3

godot-cpp version

4.2 beta3

System information

Windows 11

Issue description

When trying to create a GDExtension abstract class, the compiler fails to allow pure virtual functions, reporting:

error C2259: 'ParentClass': cannot instantiate abstract class

This is because the GDCLASS macro explicitly makes two references to where it attempts to create the class object, specifically in these two static methods:

static GDExtensionObjectPtr create(void *data) {
	m_class *new_object = memnew(m_class);
	return new_object->_owner;
}

static GDExtensionClassInstancePtr recreate(void *data, GDExtensionObjectPtr obj) {
	_GDCLASS_RECREATE(m_class, m_inherits);
}

The ability to use GDCLASS in GDNative and construct abstract classes exists. I would suggest that GDCLASS function like it does in GDNative and these two specific methods be added to either a separate macro or we introduce a new GDCLASS_ABSTRACT macro that can be used specifically for abstract class construction with all but the above 2 functions defined.

Steps to reproduce

Create a class hierarchy as follows

class ParentClass : public Resource {
  GDCLASS(ParentClass, Resource);
  static void _bind_methods() {}
  virtual void pure_virtual_method() = 0;
}

class ChildClass : public ParentClass {
  GDCLASS(ChildClass, ParentClass);
  static void _bind_methods() { }
  void pure_virtual_method() override { }
}

Minimal reproduction project

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis has been identified as a bugconfirmed

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions