Non-template class from a template base class with pure virtual methods

Discussion in 'C++' started by vilarneto@gmail.com, Mar 25, 2007.

  1. Guest

    Hello everyone,

    I'm facing a particular situation about template class derivation.
    The subject is old -- deriving a non-template class from a template
    base class -- but my problem is that the base class has a pure virtual
    method. For instance:

    template<class T>
    class A {
    public:
    virtual void method() = 0;
    };

    class B : public A<int> {
    public:
    // neither of the following worked:
    // void method() { }
    // void method<int>() { }
    // template<int> method() { }
    };

    int main(int argc, char *argv[]) {
    B b;
    return 0;
    }

    This is probably simple, but I couldn't figure out a way to implement
    the derived method.

    Also, I don't know if this matters, but I'd better stay apart from
    compiler idiosyncrasies -- I'm searching for something portable, at
    least between g++3.4.4 and VC7.

    Does anybody have any hint about this?

    --
    Best regards,
    Vilar Camara Neto
    , Mar 25, 2007
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > Hello everyone,
    >
    > I'm facing a particular situation about template class derivation.
    > The subject is old -- deriving a non-template class from a template
    > base class -- but my problem is that the base class has a pure virtual
    > method. For instance:
    >
    > template<class T>
    > class A {
    > public:
    > virtual void method() = 0;
    > };
    >
    > class B : public A<int> {
    > public:
    > // neither of the following worked:
    > // void method() { }


    Actually, the above looks right and works for me. What error do you get?

    > // void method<int>() { }
    > // template<int> method() { }
    > };
    >
    > int main(int argc, char *argv[]) {
    > B b;
    > return 0;
    > }
    >
    > This is probably simple, but I couldn't figure out a way to implement
    > the derived method.
    >
    > Also, I don't know if this matters, but I'd better stay apart from
    > compiler idiosyncrasies -- I'm searching for something portable, at
    > least between g++3.4.4 and VC7.
    >
    > Does anybody have any hint about this?


    Try the following with your compilers:

    #include <iostream>

    template<class T>
    class A {
    public:
    virtual void method() = 0;
    };

    class B : public A<int> {
    public:
    void method() { std::cout << "hi!\n"; }
    };

    int main(int argc, char *argv[]) {
    B b;
    b.method();
    return 0;
    }


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 25, 2007
    #2
    1. Advertising

  3. Guest

    On 25 mar, 14:29, Kai-Uwe Bux <> wrote:
    > > template<class T>
    > > class A {
    > > public:
    > > virtual void method() = 0;
    > > };

    >
    > > class B : public A<int> {
    > > public:
    > > // neither of the following worked:
    > > // void method() { }

    >
    > Actually, the above looks right and works for me. What error do you get?


    Shame on me, sorry. I was actually compiling+linking with gcc, which
    was giving me linking errors. The problem is that "-lstdc++" isn't
    implied when using gcc. (The fact that there were no compiling errors
    gave me the impression that gcc would behave exactly as g++, according
    to the source file extension.)

    With g++ I've got no linking errors and everything works great.

    Even so, your "this works for me" was a key point to understand the
    problem. Thank you, Bux.

    --
    Regards,
    Vilar Camara Neto
    , Mar 25, 2007
    #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.
Similar Threads
  1. qazmlp
    Replies:
    4
    Views:
    430
    David Harmon
    Feb 15, 2004
  2. Mitch Mooney
    Replies:
    2
    Views:
    371
    Bob Hairgrove
    Jun 18, 2004
  3. Replies:
    10
    Views:
    720
    Richard Herring
    Oct 18, 2005
  4. reppisch
    Replies:
    6
    Views:
    400
    Andrey Tarasevich
    May 6, 2008
  5. Hicham Mouline
    Replies:
    1
    Views:
    590
    Victor Bazarov
    Apr 20, 2009
Loading...

Share This Page