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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:15:49+00:00 2026-06-16T05:15:49+00:00

I’m starting working with C++ templates just because I wanted to understand specific differences

  • 0

I’m starting working with C++ templates just because I wanted to understand specific differences with other languages (Java) and I reached a point in which they started to diverge but I’m not getting how I am supposed to solve the specific problem (or get around it).

Suppose I have a generic value class, eg

template <class T>
class Value
{
  protected:
    T value;

  public:
    Value(Type type, T value) : type(type), value(value) {}

    void set(T value) { this->value = value; }
    T get() const { return this->value; }
    T clone() { return new Value<T>(type, value); }

    virtual string svalue() const = 0;

    const Type type;
};

and a specific subtype:

class Int : public Value<int>
{
  public:  
    Int(int value) : Value<int>(INT, value) { };

    virtual string svalue() const { ... }

  friend ostream& operator<<(ostream& os, const Int& v);
};

(I know it is also possible to specify type specific code by using template <> but since I still need to use it enough to understand it I just defined by own Int class for now, which is nothing more that a typedef Value<int> in the end)

Is it possible to have, let’s say, a collection that is able to store arbitrary pointers to Value instances? Without the need of specifying the specific concrete type of the generic class.

From what I understand templates are just a compile time issue for which the compiler analyzes all the concrete types for which the template is used and compiles different versions of the same methods for each of them, thus what I’m trying to do doesn’t seem to be possible (while in Java I am allowed to use wildcards for something like List<Value<?>>). Am I wrong?

Is there a common design to solve this issue or I am forced to drop templates to achieve it?

  • 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-16T05:15:52+00:00Added an answer on June 16, 2026 at 5:15 am
    #include <iostream>
    #include <memory>
    
    class Base 
    {
        public: virtual void Print() = 0;
    };
    
    template<typename T>
    class Derived : public Base
    {
        T V;
    public:
        void Print() { std::cout << V; }
        Derived(T v) : V(v) { }
    };
    
    int main()
    {
        std::unique_ptr<Base> Ptr (new Derived<int>(5));
        Ptr->Print();
    
        return 0;
    }
    

    I think it’s pretty self-explanatory.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.