Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4616918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:03:03+00:00 2026-05-22T02:03:03+00:00

I have an abstract Handle<T> class that contains references an objects of type T.

  • 0

I have an abstract Handle<T> class that contains references an objects of type T. I want to be able to have that class be able to be converted to Handle<U>, where U is a superclass of T. I would use inheritance, but that doesn’t work here. How would I go about doing this? What are good alternatives?

Example psuedo code:

template<class T>
class Handle {
public:
    virtual ~Handle () {}
    virtual T & operator* () const = 0;
    virtual T * operator-> () const = 0;
    virtual template<class U> operator Handle<U>* () const = 0; // being lazy with dumb pointer
};

template<class T>
class ConcreteHandle : public Handle<T> {
public:
    explicit template<class U> ConcreteHandle (U * obj) : obj(obj) {}
    virtual ~ConcreteHandle () {}
    virtual T & operator* () const {
        return *obj;
    }
    virtual T * operator-> () const {
        return obj;
    }
    virtual template<class U> operator Handle<U>* () {
        return new ConcreteHandle<U>(obj);
    }
private:
    T * obj;
};

As requested, this is what I’m doing

class GcPool {
public:
    virtual void gc () = 0;
    virtual Handle<GcObject> * construct (GcClass clazz) = 0;
};

class CompactingPool : public GcPool {
public:
    virtual void gc () { ... }
    virtual Handle<GcObject> * construct (GcClass clazz) { ... }
private:
    Handle<GcList<Handle<GcObject> > > rootSet; // this will grow in the CompactingPool's own pool
    Handle<GcList<Handle<GcObject> > > knownHandles; // this will grow in the CompactingPool's own pool.
};

knownHandles needs to be compatable with Handle so it can be in the CompatingPool’s rootSet. Same goes for rootSet. I will bootstrap these special handles so a chicken and egg problem does not occur.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:03:04+00:00Added an answer on May 22, 2026 at 2:03 am
    virtual template<class U> operator Handle<U>* () const  =0;
    

    Template virtual function is not allowed by the language specification.

    Consider this code at ideone, and then see the compilation error:

    error: templates may not be ‘virtual’


    Now what can you do? One solution is this:

    template<class T>
    class Handle {
    public:
    
        typedef typename T::super super; //U = super, which is a superclass of T.
    
        virtual ~Handle () {}
        virtual T & operator* () const = 0;
        virtual T * operator-> () const = 0;
    
        //not a template now, but still virtual
        virtual super operator Handle<super> () const = 0;  
    };
    

    That is, define a typedef of the base class in the derived class, and use it in the Handle. Something like this:

    struct Base {//...};
    
    struct Derived : Base { typedef Base super; //...};
    
    Handle<Derived>  handle; 
    

    Or you can define traits, as:

    struct Base {//... };
    
    struct Derived : Base { //... };
    
    template<typename T> struct super_traits;
    
    struct super_traits<Derived>
    {
       typedef Base super;
    };
    
    template<class T>
    class Handle {
    public:
    
        typedef typename super_traits<T>::super super; //note this now!
    
        virtual ~Handle () {}
        virtual T & operator* () const = 0;
        virtual T * operator-> () const = 0;
    
        //not a template now, but still virtual
        virtual super operator Handle<super> () const = 0; 
    };
    

    In my opinion, super_traits is a superior solution, as you’re defining the traits of derived classes without editing them. Also, you can define as many typedefs as you want; say your derived class has more than one base, you may want to define many typedefs, or preferably a typelist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Offer class (NSManagedObject subclass) that I want to use to handle
I have an abstract class called Node . It contains a constructor that takes
Problem Description I have an abstract Paper class that contains common properties of all
I have built an abstract class that is used to handle command line options
Working with C#. I have an abstract class that I use to read/write settings
I have an abstract class Camera. There are some derived classes that will handle
Let we have one abstract class: classdef ACalculation < handle methods (Abstract) [result] =
I have a abstract base C# class with a couple of methods that has
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have several queues that all extend a Queue class. I want to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.