Skip to main content
added 85 characters in body
Source Link
sebas
  • 490
  • 2
  • 7

My suggestion is to use abstract pure virtual class as Interfaces OR template policy. Your system can accept only components that inherit from a template specified interface OR that implements template policies. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

class Test
{
    std::vector<Interface *> items;
    public: 
        
        void AddComponent(T& param) 
        {
            items.push_back(&param);
        };

        void Update()
        {
            for (unsigned i = 0; i < items.size(); i++)
                items[i]->Update();
        }
};

test.AddComponent(objThatInheritsFromInterfaceTestComponent);OR

InterfaceTestComponent could, for example, have as contract an Update function.

template <class T>
class Test
{
    std::vector<T *> items;
    public: 
        
        void AddComponent(T& param) 
        {
            items.push_back(&param);
        };

        void Update()
        {
            for (unsigned i = 0; i < items.size(); i++)
                items[i]->Update();
        }
};

P.S.: you can also avoid to use the templates if you do not like it, you probably doThe second one does not need this levelthe use of abstraction

class TestSystem { public: void AddComponent(InterfaceTestComponent& param) {}; };

the interfaces, however the compiler will work as well :)not compile the code if T does not implement a public Update function.

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from a template specified interface. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

test.AddComponent(objThatInheritsFromInterfaceTestComponent);

InterfaceTestComponent could, for example, have as contract an Update function.

P.S.: you can also avoid to use the templates if you do not like it, you probably do not need this level of abstraction

class TestSystem { public: void AddComponent(InterfaceTestComponent& param) {}; };

will work as well :)

My suggestion is to use abstract pure virtual class as Interfaces OR template policy. Your system can accept only components that inherit from a specified interface OR that implements template policies. In this way your system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

class Test
{
    std::vector<Interface *> items;
    public: 
        
        void AddComponent(T& param) 
        {
            items.push_back(&param);
        };

        void Update()
        {
            for (unsigned i = 0; i < items.size(); i++)
                items[i]->Update();
        }
};

OR

template <class T>
class Test
{
    std::vector<T *> items;
    public: 
        
        void AddComponent(T& param) 
        {
            items.push_back(&param);
        };

        void Update()
        {
            for (unsigned i = 0; i < items.size(); i++)
                items[i]->Update();
        }
};

The second one does not need the use of the interfaces, however the compiler will not compile the code if T does not implement a public Update function.

added 239 characters in body
Source Link
sebas
  • 490
  • 2
  • 7

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from a template specified interface. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

test.AddComponent(objThatInheritsFromInterfaceTestComponent);

InterfaceTestComponent could, for example, have as contract an Update function.

P.S.: you can also avoid to use the templates if you do not like it, you probably do not need this level of abstraction

class TestSystem { public: void AddComponent(InterfaceTestComponent& param) {}; };

will work as well :)

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from a template specified interface. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

test.AddComponent(objThatInheritsFromInterfaceTestComponent);

InterfaceTestComponent could, for example, have as contract an Update function

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from a template specified interface. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

test.AddComponent(objThatInheritsFromInterfaceTestComponent);

InterfaceTestComponent could, for example, have as contract an Update function.

P.S.: you can also avoid to use the templates if you do not like it, you probably do not need this level of abstraction

class TestSystem { public: void AddComponent(InterfaceTestComponent& param) {}; };

will work as well :)

added 335 characters in body
Source Link
sebas
  • 490
  • 2
  • 7

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from the interface usinga template specified interface. In In this way your system will know that, no matter what, your component will implement the functionfunctions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

test.AddComponent(objThatInheritsFromInterfaceTestComponent);

InterfaceTestComponent could, for example, have as contract an Update function

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from the interface using template. In this way your system will know that, no matter what, your component will implement the function present in the interface. The system will be aware of the fact that your component has an Update function in this way.

My suggestion is to use abstract pure virtual class as Interfaces. Your system can accept only components that inherit from a template specified interface. In this way your system will know that, no matter what, your component will implement the functions present in the interface. The system will be aware of the fact that your component has an Update function in this way.

you could do something like this:

template < class T = InterfaceTestComponent > class TestSystem { public: void AddComponent(T& param) {}; };

it's 2 years that I do not touch c++ so I am sure there is a better way, nevertheless if you do

TestSystem<> test;

then you can do

test.AddComponent(objThatInheritsFromInterfaceTestComponent);

InterfaceTestComponent could, for example, have as contract an Update function

Source Link
sebas
  • 490
  • 2
  • 7
Loading