Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When register a Abstract Class using GDREGISTER_CLASS(), compiled without error #1425

Closed
pupil1337 opened this issue Mar 29, 2024 · 1 comment · Fixed by #1431
Closed

When register a Abstract Class using GDREGISTER_CLASS(), compiled without error #1425

pupil1337 opened this issue Mar 29, 2024 · 1 comment · Fixed by #1431
Labels
discussion enhancement This is an enhancement on the current functionality topic:buildsystem Related to the buildsystem or CI setup

Comments

@pupil1337
Copy link
Contributor

pupil1337 commented Mar 29, 2024

Godot version

4.3.dev(29b3d9e)

godot-cpp version

4.3.dev(a62f633)

System information

windows 11

Issue description

Created an Abstract class that inherits from Node. When I registered this class using GDREGISTER_CLASS() in register_types.cpp and compiled the project, the C++ did not report any errors.

Open the engine, and selecte this class to create a root node, it will print error:
QQ截图20240329171501

The expected result is that when I register an abstract class with GDREGISTER_CLASS(), the project cannot be compiled.

I analyzed and it seems that the following code caused the class constructor to be skipped during compilation:

// class_db.hpp
template <class T>
static GDExtensionObjectPtr _create_instance_func(void *data) {
	if constexpr (!std::is_abstract_v<T>) {
		T *new_object = memnew(T);
		return new_object->_owner;
	} else {
		return nullptr; // <- will execute this line
	}
}

Steps to reproduce

  • create an abstract class like:
class Foo : public Node {
  GDCLASS(Foo, Node)

  virtual void func() = 0;
};
  • register class in register_types.cpp:
GDREGISTER_CLASS(Foo);
  • compile the project, will successed.

  • open the engine and create this class with error

Minimal reproduction project

N/A

@AThousandShips AThousandShips added enhancement This is an enhancement on the current functionality discussion topic:buildsystem Related to the buildsystem or CI setup labels Mar 29, 2024
@pupil1337
Copy link
Contributor Author

pupil1337 commented Mar 29, 2024

When my class A inherits an abstract class X, my goal was to implement pure virtual methods. When forget implement pure virtual methods, because _create_instance_func hide the memnew, led to skip create instance, So there is no compilation error to prompt me, but usually new A() has an error prompt like "Allocating an object of abstract class".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement This is an enhancement on the current functionality topic:buildsystem Related to the buildsystem or CI setup
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants