Thread: resolving virtual class for Queue?

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    Use a std::queue<Foo*> or std::queue<std::tr1::shared_ptr<Foo> > instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:05 PM
  5. virtual prototype in a class
    By sphreak in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 02:34 PM