resolving virtual class for Queue?

This is a discussion on resolving virtual class for Queue? within the C++ Programming forums, part of the General Programming Boards category; How does one go about resolving a virtual class so that the STL class queue will be able to instantiate? ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    42

    resolving virtual class for Queue?

    How does one go about resolving a virtual class so that the STL class queue will be able to instantiate?

    Code:
    #include <queue>
    
       class Foo {
                virtual int run()  = 0;
      };
    
      int main() {
           
           queue<Foo> fooQueue;
    
      }
    I need the virtual class so that a user may implement their own class to pass in as a reference. Any ideas?

    Thanks in advance.
    Ken

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by bean66 View Post
    I need the virtual class so that a user may implement their own class to pass in as a reference. Any ideas?
    You can't instantiate an abstract virtual class. So why would you think you could store them in a container?

    You need to store pointers, not objects. And that means object lifetimes need to be managed somehow.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Use a std::queue<Foo*> or std::queue<std::tr1::shared_ptr<Foo> > instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    I'm obviously new to C++ and learning.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  3. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  4. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  5. virtual prototype in a class
    By sphreak in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21