Thread: Covariance with virtual functions and stl containers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Covariance with virtual functions and stl containers

    I have two classes, one derived from the other, and a virtual get function is supposed to return a pointer to a container with different element types depending on who's getting called. Simplified:

    Code:
    class A {
    public:
    virtual stl_container<T1>* get_something();
    };
    
    class B : public A {
    public:
    stl_container<T2>* get_something();
    };
    And no, the classes themselves are not templates.

    Anyway, when I try to compile this, it gives me an error message about "invalid covariant return types." So I thought it might help if the containers were recast into an isa relationship or something:

    Code:
    template <typename C = T1>
    class C1 : public stl_container<C> {};
    
    class C2 : public C1<T2> {};
    
    class A {
    public:
    virtual C1<>* get_something();
    };
    
    class B : public A {
    public:
    C2* get_something();
    };
    But that didn't work either.

    I'm almost tempted to return a void* and cast it as it comes out. Is there another solution?

    Oh, the reason the simplest solution (turning class A and B into a templated class) is not possible because the T1, T2 types are custom classes and different things are supposed to happen to them depending on the class.
    Last edited by cunnus88; 03-31-2009 at 07:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind on member functions of stl containers
    By synss in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2008, 04:38 AM
  2. Replies: 2
    Last Post: 10-02-2005, 05:04 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM