1

I don't understand att all inheritance with templates..

template <typename T> 
class Mere 
{ 
protected:
    Mere();
}; 

class Fille2 : public Mere<int>
{ 
protected:
    Fille2(){
        Mere();
    }
}; 


int main(int argc, char** argv) {

    return 0;
}

Why do I have this error?

main.cpp:22:5: error: 'Mere<T>::Mere() [with T = int]' is protected
Mere();

And all works when I put "Mere()" in public? I can't have "protected" functions for my class "Mere"? Why?

E A
  • 13
  • 2
  • 3
    this is not how you invoke constructors of a parent class in c++... see [here](http://melpon.org/wandbox/permlink/DtiZ3q60YyVytvju) on how it can be done – W.F. Dec 13 '16 at 20:37

1 Answers1

3

Yes, you can call the base class constructor, even if it is protected. Here is the correct syntax:

class Fille2 : public Mere<int>
{ 
protected:
    Fille2(): Mere() {
    }
}; 

For a detailed discussion, see Why is protected constructor raising an error this this code?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012