how to call an inherited, template class constructor from initializerlist of an inheriting, non-temp

Discussion in 'C++' started by l.s.rockfan@web.de, Nov 14, 2008.

  1. Guest

    Hello,

    how do i have to call an inherited, templated class constructor from the
    initializer list of the inheriting, non-templated class constructor?

    example code:


    template<typename T>
    class A
    {
    protected:
    sometype* something;
    T something_else; /*gives the template some sense here*/
    public:
    A(sometype* param) : something(param) {};
    }


    class B : public A<int>
    {
    public:
    B(sometype* param) : A(param) {}; // <== Compiler Error

    /* further member functions */
    }


    The compiler always tries to identify A as a member variable not being
    found, instead of the base class' constructor.
     
    , Nov 14, 2008
    #1
    1. Advertising

  2. Guest

    Obnoxious User wrote:
    > On Fri, 14 Nov 2008 21:28:53 +0100, wrote:
    >
    >> Hello,
    >>
    >> how do i have to call an inherited, templated class constructor from the
    >> initializer list of the inheriting, non-templated class constructor?
    >>
    >> example code:
    >>
    >>
    >> template<typename T>
    >> class A
    >> {
    >> protected:
    >> sometype* something;
    >> T something_else; /*gives the template some sense here*/
    >> public:
    >> A(sometype* param) : something(param) {};
    >> }
    >>
    >>
    >> class B : public A<int>
    >> {
    >> public:
    >> B(sometype* param) : A(param) {}; // <== Compiler Error
    >>

    >
    > B(sometype * param) : A<int>(param) {}
    >


    Thanks.

    Mysterious, it really works.

    All the time I tried it before, I coded it outside the class body like

    B::B(sometype* param) : A<int>(param) {}

    and it didn't work because of an ld error.


    When I move it inside the class body (like in my example code above) it
    works.

    Could someone explain this behaviour to me, please?
     
    , Nov 14, 2008
    #2
    1. Advertising

  3. Guest

    Obnoxious User wrote:
    > On Fri, 14 Nov 2008 21:28:53 +0100, wrote:
    >
    >> Hello,
    >>
    >> how do i have to call an inherited, templated class constructor from the
    >> initializer list of the inheriting, non-templated class constructor?
    >>
    >> example code:
    >>
    >>
    >> template<typename T>
    >> class A
    >> {
    >> protected:
    >> sometype* something;
    >> T something_else; /*gives the template some sense here*/
    >> public:
    >> A(sometype* param) : something(param) {};
    >> }
    >>
    >>
    >> class B : public A<int>
    >> {
    >> public:
    >> B(sometype* param) : A(param) {}; // <== Compiler Error
    >>

    >
    > B(sometype * param) : A<int>(param) {}
    >


    Thanks.

    Mysterious, it really works.

    All the time I tried it before, I coded it outside the class body like

    B::B(sometype* param) : A<int>(param) {}

    and it didn't work because of an ld error: undefined reference to
    A<int>::A(param).

    But when I move it inside the class body (like in my example code above)
    it works.

    Could someone explain this behaviour to me, please?
     
    , Nov 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page