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

  • Home
  • SEARCH
  • 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 9106977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:27:49+00:00 2026-06-17T02:27:49+00:00

I would like to have several types that share the same implementation but still

  • 0

I would like to have several types that share the same implementation but still are of different type in C++.

To illustrate my question with a simple example, I would like to have a class for Apples, Oranges and Bananas, all having the same operations and same implementation. I would like them to have different types because I want to avoid errors thanks to type-safety.

class Apple {
     int p;
public:
     Apple (int p) : p(p) {}
     int price () const {return p;}
}

class Banana {
     int p;
public:
     Banana (int p) : p(p) {}
     int price () const {return p;}
}

class Orange ...

In order not duplicating code, it looks like I could use a base class Fruit and inherit from it:

class Fruit {
     int p;
public:
     Fruit (int p) : p(p) {}
     int price () const {return p;}
}

class Apple: public Fruit {};
class Banana: public Fruit {};
class Orange: public Fruit {};

But then, the constructors are not inherited and I have to rewrite them.

Is there any mechanism (typedefs, templates, inheritance…) that would allow me to easily have the same class with different types?

  • 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-06-17T02:27:51+00:00Added an answer on June 17, 2026 at 2:27 am

    A common technique is to have a class template where the template argument simply serves as a unique token (“tag”) to make it a unique type:

    template <typename Tag>
    class Fruit {
        int p;
    public:
        Fruit(int p) : p(p) { }
        int price() const { return p; }
    };
    
    using Apple = Fruit<struct AppleTag>;
    using Banana = Fruit<struct BananaTag>;
    

    Note that the tag classes don’t even need to be defined, it’s enough to declare a unique type name. This works because the tag isn’s actually used anywhere in the template. And you can declare the type name inside the template argument list (hat tip to @Xeo).

    The using syntax is C++11. If you’re stuck with C++03, write this instead:

    typedef Fruit<struct AppleTag> Apple;
    

    If the common functionality takes up a lot of code this unfortunately introduces quite a lot of duplicate code in the final executable. This can be prevented by having a common base class implementing the functionality, and then having a specialisation (that you actually instantiate) that derives from it.

    Unfortunately, that requires you to re-implement all non-inheritable members (constructors, assignment …) which adds a small overhead itself – so this only makes sense for large classes. Here it is applied to the above example:

    // Actual `Fruit` class remains unchanged, except for template declaration
    template <typename Tag, typename = Tag>
    class Fruit { /* unchanged */ };
    
    template <typename T>
    class Fruit<T, T> : public Fruit<T, void> {
    public:
        // Should work but doesn’t on my compiler:
        //using Fruit<T, void>::Fruit;
        Fruit(int p) : Fruit<T, void>(p) { }
    };
    
    using Apple = Fruit<struct AppleTag>;
    using Banana = Fruit<struct BananaTag>;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several types that share common behaviour and with same constructors and operators.
I would like to define several versions of a thing but with different types
I have several PDF templates that I would like to load and modify and
I have several columns in a MySQL database that I would like to add
I am playing with making a blog. I would like to have several types
I'd like to add several different user types to my rails application. I have
I have several UITableViews and what I would like to do is to be
I have several ASP.NET sites in IIS7 and would like to be able to
If I have several plots in a single panel, I would like to be
I have several python.exe processes running on my Vista machine and I would like

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.