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 980471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:23:11+00:00 2026-05-16T04:23:11+00:00

Is it possible to obtain a reference/pointer to a class type and enforce that

  • 0

Is it possible to obtain a reference/pointer to a class type and enforce that it derives from a particular base class?

I’m writing a client library that needs to negotiate with a server to pick an algorithm to use for communication. I want the user of the library to be able to select a subset of algorithms to use and not be fixed to the set I initially provide (ie. not fixed in some kind of factory class).

Ideally this would be done by passing in a list of classes that derive from some common “Algorithm” subtype. I’ve seen the “Type” object but I would have to check all the types myself. Is there a way to have the compiler do this for me? What I want is something like “Type<Algorithm>” but I can’t find anything like that. Or is there different way entirely to do this?

An example of what I’ve thought of so far:

public class Algorithm {
    public static abstract Name;
}

public class Client {
    public MyLib(Type[] algorithms) {
      m_algorithms = algorithms;
      // ... Check they all derive from Algorithm
    }

    public Communicate() {
      // ... Send list of algorithm names to server
      // ... Create instance of algorithm dictated by server response
    }
}
  • 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-16T04:23:12+00:00Added an answer on May 16, 2026 at 4:23 am

    You unfortunately can’t check that the types are all Algorithms at compile time. This is one of the (few) features I miss about Java. However, there are some good workarounds, which (depending on your situation) may be better than the solution you were hoping for. For example:

    public abstract class Algorithm {
    }
    
    public class AlgorithmA : Algorithm { }
    public class AlgorithmB : Algorithm { }
    
    public interface IAlgorithmFactory
    {
        string Name {get;}
        Algorithm GetAlgorithm();
    }
    
    public class AlgorithmFactory<T> : IAlgorithmFactory where T : Algorithm, new()
    {
        public string Name {get { return typeof(T).Name; }}
        public Algorithm GetAlgorithm()
        {
            return new T();
        }
    }
    
    public class Client {
        public void MyLib(IEnumerable<IAlgorithmFactory> algorithms) {
    
        // ... Check they all derive from Algorithm
        }
    
        public void Communicate() {
        // ... Send list of algorithm names to server
        // ... Create instance of algorithm dictated by server response
        }
    }
    

    Then you can use your client class like this:

    new Client().MyLib(new IAlgorithmFactory[] 
                       {
                           new AlgorithmFactory<AlgorithmA>(), 
                           new AlgorithmFactory<AlgorithmB>()
                       });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using reflection it is possible to obtain the call stack (apart from that
Possible Duplicate: Getting Filename from file descriptor in C Obtain filename from file pointer
Is it possible to obtain a Class reference to a Vector.<T> ? I tried
Is it possible to obtain styled attributes values from particular Theme without setting the
is it possible to obtain the instance name of a class from the class
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Consider number 194 declared as type int Is it possible to obtain it's digits
Is it possible to obtain ServletContext from MyPortlet extends MVCPortlet in Liferay? I know
Is it possible to obtain values that match a range of keys in Java
Is it possible to obtain an instance of Class<SomeGenericClass<SomeType>> ? For example an instance

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.