Home > Blockchain >  What is the proper syntax in C for template classes with pointer template types with reference var
What is the proper syntax in C for template classes with pointer template types with reference var

Time:05-25

If I have a template class like this:

template <class T>
class MyClass
{
    public:
    virtual void MyFunction(const T& t)
    {
        ///
    }
};

How will MyFunction look like in a concrete class with a pointer T?

class Data;
class MyDerivedClass : public MyClass<Data*>
{
    public:
    void MyFunction(???) override
    {
        ///
    }
};

Also, how should I use the variable?

CodePudding user response:

If you really want this setup, you can.

Example:

class MyDerivedClass : public MyClass<Data*> {
public:
    using value_type = Data*;   // alias to simplify overriding

    void MyFunction(const value_type& dpr) override {
        // ....
    }
};

how should I use the variable?

You'd use it like any other pointer. Note that it's not the Data instance that is const, it's the pointer itself.

CodePudding user response:

You should try:

void MyFunction(Data * const & i) override

But also consider using the template instead of a derived class.

  • Related