There are two things you need to know:
First, that any common function may return an object, as long as the object has a callable copy constructor. So, if I have some class MyClass, I could create a ordinary function that returns an instance of it:
MyClass create_instance(int val) {
{
MyClass inst(val + 42); // Call a constructor
// Maybe do something else:
// ...
return inst; // Return a copy of the object created.
}
So this solves the problem of creating the object without using a constructor directly: in this case, just call create_instance()
and takes the return.
Second, that functions called without an instance (i.e. like ClassName::function_name();
instead of object.function_name();
) are static member functions, and the only way they differ from ordinary functions is that they can access the private and protected members of the objects of that class, like ordinary member functions.
So all you need to do is create a function that instantiate and returns a Polygon
, that also happens to be a static member function of Polygon
.