Search code examples
c++syntaxsfml

How is implemented this function foo.getSize().x


Recently I've start learning sfml graphics and I saw this kind of functions window.getSize().x or .y, my question is how can I write such a function, more exactly to use .x or .y on an object function?


Solution

  • Those functions return a vector object that have the variables x and y internally.

    Something kinda like this

    template<typename T>
    struct Vec2
    {
        T x, y;
    
        Vec2(T x, T y)
            : x(x), y(y)
    };
    
    class Window
    {
    public:
        Vec2<unsigned int> getSize()
        {
            return size;
        }
    private:
        Vec2<unsigned int> size;
    };
    
    

    This is only a simple example but it should show you how it works.