CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2002
    Posts
    6

    Inheriting from a template class that has a pure virtual function

    I was wondering what is the syntax, if there is any to inherit from a template class that has a pure virtual function

    template <class base> class baseclass
    {
    void purevirtualfunc() =0;

    int value;
    };

    class derivedclass : public "what is correct syntax?"
    {
    blah...
    };

    I have something like this in my program "class TestQueue : public Queue<QueueClass> Queue," but then I get "QueueQlass undeclared identifier."

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    I don't know if this answers your question, but it compiles:

    Code:
    template <class T> class baseclass
    {
    public:
    	virtual void purevirtualfunc() = 0;
    	T value;
    };
    
    class derivedclass : public baseclass<int>
    {
    	virtual void purevirtualfunc()
    	{
    		value = 1;
    	}
    };
    Jeff

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Or you could just do:
    Code:
    template <class T>
    class baseclass
    {
        // whatever
    };
    
    template <class T>
    class derivedclass : public baseclass<T>
    {
        // whatever
    };
    Last edited by Graham; August 7th, 2002 at 02:43 PM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Aug 2002
    Posts
    6
    Wow. Both solutions worked. Thanks a lot.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Jeff: I'm not sure how to react to an expression of surprise that something we suggested worked!
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Well, Brian did say thanks, so I choose to overlook the astonishment and take it as a compliment. And, hey, I'm still surprised my stuff works!

    s,

    Jeff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured