user1394884
user1394884

Reputation: 211

c++ How do you call a templated base-class function from derived class instance

Found related questions but not the exact variant so I am posting a very simple question. A derived class inherits from a templated base, and I want to call the base function, how to do it?

template <class A>
class testBase {
public:
    void insert(const A& insertType) {
         // whatever
    }
};

class testDerived : testBase<double> {
     // whatever
};


int main() {

    testDerived B;

    // Compiler doesn't recognize base class insert
    // How do you do this?
    B.insert(1.0);
}

Upvotes: 1

Views: 157

Answers (2)

stinky472
stinky472

Reputation: 6797

A class has a default access level of 'private'. You basically inherited 'testBase' using private inheritance so that testBase's public interface is not part of testDerived's. Simple solution:

class testDerived: public testBase<double> {...};

I do wish C++ applied public inheritance by default though since that's generally a much more common case. Then again, we could just all use structs instead. :-D

Upvotes: 2

hmjd
hmjd

Reputation: 121961

Need public inheritance (default is private for class):

class testDerived : public testBase<double> {

Upvotes: 4

Related Questions