简体   繁体   中英

can i pass object class that is inherited from template class?

How can i pass object that inherited from template as this to function

please see the GameObj::GameObj constructor i try to pass the GameObj that is inheriting the BaseGameObject template but its not valid

template<typename T>
class BaseGameObject 
{
    public:
        BaseGameObject(){};

        virtual ~BaseGameObject(){};

        static T* create(IImageComponent* imageComponent)
        {

        }
    protected:
        IImageComponent* m_IImageComponent;
};
class GameObj :public BaseGameObject<GameObj>  
{
    public:
        GameObj(IImageComponent* imageComponent);
        virtual ~GameObj(){};



};

GameObj::GameObj(IImageComponent* imageComponent):m_IImageComponent(imageComponent)
{

    m_IImageComponent->update(*this); //HERE IS THE PROBLEM IT ASK ME TO PASS TAMPLATE 
}
class GameObjImageCompnent
{
public :
    GameObjImageCompnent(const std::string &spritefilename);
    virtual void update(BaseGameObject& baseGameObject);


private:
    std::string m_spritefilename;
};

GameObjImageCompnent::GameObjImageCompnent(const std::string &spritefilename):
    m_spritefilename(spritefilename)
{
    ;
}
void GameObjImageCompnent::update(BaseGameObject& baseGameObject)
{

    baseGameObject.setInitWithSpriteFrameName(m_spritefilename);
}

this link doesn't work for me : Passing template classes as arguments to methods

BaseGameObject is a template. GameObjImageCompnent::update has a declaration virtual void update(BaseGameObject& baseGameObject); . That's wrong because BaseGameObject is not a type but a template.

Maybe you should change that declaration to: virtual void update(BaseGameObject<GameObj>& baseGameObject);

If you need to be able to call that with different BaseGameObject s, you could make GameObjImageCompnent into a template as well:

template<class T>
class GameObjImageCompnent {
// ...
virtual void update(BaseGameObject<T>& baseGameObject);

If that's not an option, you probably need to inherit BaseGameObject from a non-template base class and and use that as your reference type. If a base class for BaseGameObject is not an option either, you need to rethink your design.

class IGameObject {
public:
    virtual ~IGameObject() {}
    // any other virtual functions that are needed
};

template<typename T>
class BaseGameObject: public IGameObject {
//...

class GameObjImageCompnent {
// ...
virtual void update(IGameObject& baseGameObject);

You seem to be using CRTP . See the pitfalls section:

One issue with static polymorphism is that without using a general base class like "Shape" from the above example, you cannot store your derived classes heterogeneously as each CRTP base class is a unique type. For this reason, it's likely that you'll want to inherit from a shared base class with a virtual destructor, like the example above.

I think that limitation also applies to passing objects of derived classes to a function through a heterogeneous reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM