S
S
syxme2021-04-16 10:22:18
C++ / C#
syxme, 2021-04-16 10:22:18

A class inherited from a generic class how to pass it through a function parameter?

There is a class of generic type:

template<class T = ViewHolder>
class Adapter {
public:
    virtual T *onCreateViewHolder(ViewGroup *parent, int viewType) = 0;
    virtual void onBindViewHolder(T *holder, int position) = 0;
    T *createViewHolder(ViewGroup *parent, int viewType) {
        return onCreateViewHolder(parent, viewType);
    }
    void bindViewHolder(T *holder, int position) {
        onBindViewHolder(holder, position);
    }
    virtual int getItemViewType(int position) { return 0; }
    virtual int getItemCount() = 0;
    virtual void onViewRecycled(T *holder) {};
}


Child class class:
class MainHolder:public ViewHolder{
    public:
        MainHolder(View* itemView):ViewHolder(itemView){
            pool_id = (TextView*)itemView->findById("pool_id");
        }
};


class TestAdapter:public Adapter<MainHolder> 
{
    
    public:

        TestAdapter(Context* ctx);

        MainHolder* onCreateViewHolder(ViewGroup* parent, int viewType) override;
        int getItemCount() override;
        void onBindViewHolder(MainHolder* holder, int position) override;
        ~TestAdapter();
};


I want to pass this class as in java, for example
Adapter *mAdapter = nullptr;
void setAdapter(Adapter *adapter){
 mAdapter = adapter;
 mAdapter->getItemCount()
}

But it just happens like this:
Adapter<ViewHolder> *mAdapter = nullptr;
void setAdapter(Adapter<ViewHolder> *adapter){
//....
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question