Home > Back-end >  How to call the base class method in C ?
How to call the base class method in C ?

Time:03-30

I am working on a C program. For one of the use case, I have a class which is derived from its template class. So, I'm wondering how we can call the base class method inside the derived class method?

Example:

template <typename base>
struct derived : public base
{
    void aFunction()
    {
        // need to call a base function() here
    }
};

One of the way could be something like base::aBaseFunction(), but I am not sure?

I am new to OOP programming, so looking forward to learning a new concept through this problem statement.

CodePudding user response:

If you want to explicitly use the base's member, make the type explicit like you found:

template <typename base>
struct derived : public base
{
    void aFunction()
    {
        base::function();
    }
};

If you would rather have the usual unqualified-lookup behaviour, make this explicit instead:

template <typename base>
struct derived : public base
{
    void aFunction()
    {
        this->function();
    }
};

Both of these behave exactly like they do in non-templates.

  • Related