pure virtual function in template class

Discussion in 'C++' started by Mike -- Email Ignored, Jul 10, 2008.

  1. Is a pure virtual function in allowed in a template
    base class? In any case, I have one working. Am I
    skating on thin ice?
    Thanks,
    Mike.
     
    Mike -- Email Ignored, Jul 10, 2008
    #1
    1. Advertising

  2. On Jul 10, 8:49 am, Mike -- Email Ignored <>
    wrote:
    > Is a pure virtual function in allowed in a template
    > base class? In any case, I have one working. Am I
    > skating on thin ice?
    > Thanks,
    > Mike.



    Yes, it can be in the template class, but it can NOT be a virtual
    template member function
     
    puzzlecracker, Jul 10, 2008
    #2
    1. Advertising

  3. On Thu, 10 Jul 2008 05:53:45 -0700, puzzlecracker wrote:

    > On Jul 10, 8:49 am, Mike -- Email Ignored <>
    > wrote:
    >> Is a pure virtual function in allowed in a template base class? In any
    >> case, I have one working. Am I skating on thin ice?
    >> Thanks,
    >> Mike.

    >
    >
    > Yes, it can be in the template class, but it can NOT be a virtual
    > template member function


    I don't understand how it could be pure, but not a member.
    In any case, here is code that works on my system showing
    exactly what I mean.
    Mike.

    // virt_temp.cc 07/10/08
    #include <iostream>
    using namespace std;

    template <class TYP>
    class BaseT
    {
    protected:
    BaseT(TYP x,TYP y) : x_(x),y_(y){}
    void doAll(){cout << "x_="<<x_<<',';doChild(y_);cout<<endl;}
    virtual void doChild(TYP a)=0;// pure virtual member function
    private:
    TYP x_;
    TYP y_;
    };

    class Child : protected BaseT<int>
    {
    public:
    Child() : BaseT<int>(1,2){}
    void doThings(){doAll();}
    private:
    virtual void doChild(int a);
    };

    void Child::doChild(int a){cout<<"a="<<a;}

    int main(int argc, const char* argv[])
    {
    Child child;
    child.doThings();
    }
     
    Mike -- Email Ignored, Jul 10, 2008
    #3
  4. On Thu, 10 Jul 2008 10:56:16 -0400, Victor Bazarov wrote:

    [...]
    >
    > It's the case when too much information actually hurt. What clacker is
    > telling you is that you can't have a template member declared virtual
    > (pure or not):
    >
    > class foo {
    > template<class T> virtual void bar(T const&); // error
    > };
    >
    > , that's, all.
    >
    > V


    Then my working example is just dumb luck?
    Or might it be a non-standard gnu add-on?
    Mike.
     
    Mike -- Email Ignored, Jul 10, 2008
    #4
  5. On Thu, 10 Jul 2008 12:48:36 -0400, Victor Bazarov wrote:

    > Mike -- Email Ignored wrote:
    >> On Thu, 10 Jul 2008 10:56:16 -0400, Victor Bazarov wrote:
    >>
    >> [...]
    >>> It's the case when too much information actually hurt. What clacker
    >>> is telling you is that you can't have a template member declared
    >>> virtual (pure or not):
    >>>
    >>> class foo {
    >>> template<class T> virtual void bar(T const&); // error
    >>> };
    >>>
    >>> , that's, all.
    >>>
    >>> V

    >>
    >> Then my working example is just dumb luck?

    >
    > "Dumb luck"? I am not sure how that is applicable here. Your example
    > has no virtual functions that are member templates.


    Then what is my function:

    virtual void doChild(TYP a)=0;// pure virtual member function

    Why is this ok?

    [...]

    Mike.
     
    Mike -- Email Ignored, Jul 10, 2008
    #5
  6. On Thu, 10 Jul 2008 13:28:40 -0400, Victor Bazarov wrote:

    > Mike -- Email Ignored wrote:

    [...]
    >>
    >> Then what is my function:
    >>
    >> virtual void doChild(TYP a)=0;// pure virtual member function

    >
    > It's a pure virtual function, a member of the class template. Since it
    > is a member of a template, it is a template itself, but it's not a
    > member template. It's a template member. Confusing, isn't it?
    >

    [...]

    Yes, thanks, it is clear now (pardon my laughter). Is there somewhere
    is the standard I might see this?

    Mike.
     
    Mike -- Email Ignored, Jul 10, 2008
    #6
  7. Mike -- Email Ignored

    James Kanze Guest

    On Jul 10, 2:49 pm, Mike -- Email Ignored <>
    wrote:
    > Is a pure virtual function in allowed in a template base
    > class? In any case, I have one working. Am I skating on thin
    > ice?


    No. Why should there be any problem? The instantiation of a
    class template is just like any other class. The same rules
    apply.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 11, 2008
    #7
  8. On Fri, 11 Jul 2008 02:08:45 -0700, James Kanze wrote:

    > On Jul 10, 2:49 pm, Mike -- Email Ignored <>
    > wrote:
    >> Is a pure virtual function in allowed in a template base class? In any
    >> case, I have one working. Am I skating on thin ice?

    >
    > No. Why should there be any problem? The instantiation of a class
    > template is just like any other class. The same rules apply.


    How about, then, a template class virtual member function that
    is not pure? It is my understanding that such a virtual function
    may not be inline, but, at least on the gnu c++ compiler, non-pure
    functions of a template class must be inline (unless there have
    been recent developments I do not know about).

    Mike.
     
    Mike -- Email Ignored, Jul 13, 2008
    #8
  9. Mike -- Email Ignored

    James Kanze Guest

    On Jul 13, 3:36 am, Mike -- Email Ignored <>
    wrote:
    > On Fri, 11 Jul 2008 02:08:45 -0700, James Kanze wrote:
    > > On Jul 10, 2:49 pm, Mike -- Email Ignored <>
    > > wrote:
    > >> Is a pure virtual function in allowed in a template base
    > >> class? In any case, I have one working. Am I skating on
    > >> thin ice?


    > > No. Why should there be any problem? The instantiation of
    > > a class template is just like any other class. The same
    > > rules apply.


    > How about, then, a template class virtual member function that
    > is not pure?


    Again, the same rules apply as with any other class. No
    problem.

    > It is my understanding that such a virtual function
    > may not be inline,


    Why not? One idiom (which used to be common, before templates)
    even requires them to be inline.

    > but, at least on the gnu c++ compiler, non-pure functions of a
    > template class must be inline (unless there have been recent
    > developments I do not know about).


    Nonsense. G++ has never had this restriction.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 13, 2008
    #9
    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. IK
    Replies:
    2
    Views:
    612
    hemraj
    Jul 23, 2004
  2. YellowMaple
    Replies:
    3
    Views:
    515
    Gianni Mariani
    Mar 24, 2007
  3. Replies:
    2
    Views:
    564
  4. Replies:
    7
    Views:
    591
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    364
    dasjotre
    Jun 28, 2007
Loading...

Share This Page