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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:20:16+00:00 2026-05-12T05:20:16+00:00

This is best described in pseudo-code: class Thing {}; interface ThingGetter<T extends Thing> {

  • 0

This is best described in pseudo-code:

class Thing {};

interface ThingGetter<T extends Thing> {
    T getThing();
}

class Product extends Thing {};

class ProductGetter<Product> {
    Product getThing() {
        // Some product code
    }
}

class SpecialProductGetter extends ProductGetter {
    Product getThing() {
        p = ProductGetter::getThing();
        // Do some special stuff;
        return p;
    }
}

class NeatProductGetter extends ProductGetter {
    Product getThing() {
        p = ProductGetter::getThing();
        // Do some neat stuff;
        return p;
    }
}

I will have other “things” with other getters for those as well.

I’ve tried codifying this in C++ but it doesn’t like :

template <> class ThingGetter <Product> {

nor:

class NeatProductGetter : public ThingGetter <Product> {
  1. Can you model this in C++ without making getThing return a Thing and then having to cast it like crazy?
  2. If so, how? If not, what is the best way to do this?

Thanks!

  • 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-12T05:20:16+00:00Added an answer on May 12, 2026 at 5:20 am

    Since the question comes from someone with a Java background, I will interpret it in Java terms. You want to define a generic interface that will return an object derived from T:

    template<typename T>
    struct getter
    {
       virtual T* get() = 0; // just for the signature, no implementation
    };
    

    Note the changes: the function is declared virtual so that it will behave polimorphically in derived objects. The return value is a pointer instead of an object. If you keep an object in your interface, the compiler will slice (cut the non-base part of the returned object) the return. With some metaprogramming magic (or resorting to boost) you can have the compiler test the constraint that T derives from a given type.

    Now the question is why you would like to do so… If you define a non-generic interface (abstract class) that returns a Thing by pointer you can just get the same semantics more easily:

    struct Thing {};
    struct AnotherThing : public Thing {};
    struct getter
    {
       virtual Thing* get() = 0;
    };
    struct AnotherGetter : public getter
    {
       virtual AnotherThing* get() { return 0; }
    };
    struct Error : public getter
    {
        int get() { return 0; } // error conflicting return type for get()
    };
    struct AnotherError : public getter
    {
    };
    int main() {
       AnotherError e; // error, AnotherError is abstract (get is still pure virtual at this level)
    }
    

    The compiler will require all instantiable classes derived from getter to implement a get() method that returns a Thing by pointer (or a covariant return type: pointer to a class derived from Thing). If the derived class tries to return another type the compiler will flag it as an error.

    The problem, as you post it is that if you use the interface, then the objects can only be handled as pointers to the base Thing class. Now, whether that is a problem or not is another question… In general, if you have correctly designed your hierarchy, you should be able to use the returned objects polimorphically without having to resort to down casting.

    Note that if you are using the different getters at the most derived level in the hierarchy, each get() method returns the most derived element from the Thing hierarchy, at that point you will not need to down cast at all. Only if you use the different getters through the base getter interface you will get the same return type for all (Thing*)

    It is important to note that templates are resolved completely at compile time. That means that trying to use templates to solve the need (do you really need it?) to downcast will not help you if you use the different getters through the interface (reference/pointers to the basic getter).

    Maybe posting a little more details on your specific domain, where and how you intend to use this code, can help in providing more useful responses.

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

Sidebar

Ask A Question

Stats

  • Questions 175k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the actual information is strings, this won't work. Pack("d"… May 12, 2026 at 2:59 pm
  • Editorial Team
    Editorial Team added an answer If you're using the last version of Selenium RC (after… May 12, 2026 at 2:59 pm
  • Editorial Team
    Editorial Team added an answer Yes, this is possible using fsCommand() in your action script… May 12, 2026 at 2:59 pm

Related Questions

Recently I have been studying recursion; how to write it, analyze it, etc. I
I'm a relatively new employee at my current company, so I'm still drinking from
I think my question is best described as an example. Let's say I have
I am designing a psychology experiment with java applets. I have to make my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.