Skip to content

Reflect a class

pribault edited this page Jan 28, 2023 · 2 revisions

How to reflect a class

Class reflection is enabled by the Reflectable class and controled using macros.
To reflect a class you have to:

  • include the <CppReflection/Reflectable.h>
  • make your class inherit from the CppReflection::Reflectable class
  • start reflection using the START_REFLECTION macro
  • reflect attributes using the REFLECT_ATTRIBUTE macro
  • end reflection using the END_REFLECTION macro

Example

#include <CppReflection/Reflectable.h>

using namespace	CppReflection;
class	Point : public Reflectable
{
	public:

		START_REFLECTION(Point)
		REFLECT_ATTRIBUTE(x)
		REFLECT_ATTRIBUTE(y)
		END_REFLECTION()

	private:

		int	x;
		int	y;
};

class	Rectangle : public Point
{
	public:

		START_REFLECTION(Rectangle, Point)
		REFLECT_ATTRIBUTE(w)
		REFLECT_ATTRIBUTE(h)
		END_REFLECTION()

	private:

		int	w;
		int	h;
};

Clone this wiki locally