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

The Archive Base Latest Questions

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

I am trying to solve a programming problem that consists of an object (call

  • 0

I am trying to solve a programming problem that consists of an object (call it Diagram), that contains several parameters. Each parameter (the Parameter class) can be one of several types: int, double, complex, string – to name a few.

So my first instinct was to define my Diagram class as having a vector of template parameters, which would look like this.

class Diagram
{
private:
    std::vector<Parameter<T> > v;
};

This doesn’t compile, and I understand why. So, based on the recommendations on this page How to declare data members that are objects of any type in a class, I modified my code to look like:

class ParameterBase
{
public:
    virtual void setValue() = 0;
    virtual ~ParameterBase() { }
};


template <typename T>
class Parameter : public ParameterBase
{
public:
    void setValue() // I want this to be 
                    // void setValue(const T & val)
    {
        // I want this to be 
        // value = val;
    }

private:
    T value;
};

class Diagram
{
public:
    std::vector<ParameterBase *> v;
    int type;
};

I’m having trouble figuring out how to call the setValue function with an appropriate templated parameter. It is not possible to have a templated parameter in the ParameterBase abstract base class. Any help is greatly appreciated.

P.S. I don’t have the flexibility to use boost::any.

  • 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:07:47+00:00Added an answer on June 16, 2026 at 5:07 am

    You got very close. I added a few bits because they’re handy

    class ParameterBase
    {
    public:
        virtual ~ParameterBase() {}
        template<class T> const T& get() const; //to be implimented after Parameter
        template<class T, class U> void setValue(const U& rhs); //to be implimented after Parameter
    };
    
    template <typename T>
    class Parameter : public ParameterBase
    {
    public:
        Parameter(const T& rhs) :value(rhs) {}
        const T& get() const {return value;}
        void setValue(const T& rhs) {value=rhs;}    
    private:
        T value;
    };
    
    //Here's the trick: dynamic_cast rather than virtual
    template<class T> const T& ParameterBase::get() const
    { return dynamic_cast<const Parameter<T>&>(*this).get(); }
    template<class T, class U> void ParameterBase::setValue(const U& rhs)
    { return dynamic_cast<Parameter<T>&>(*this).setValue(rhs); }
    
    class Diagram
    {
    public:
        std::vector<ParameterBase*> v;
        int type;
    };
    

    Diagram can then do stuff like these:

    Parameter<std::string> p1("Hello");
    v.push_back(&p1);
    std::cout << v[0]->get<std::string>(); //read the string
    v[0]->set<std::string>("BANANA"); //set the string to something else
    v[0]->get<int>(); //throws a std::bad_cast exception
    

    It looks like your intent is to store resource-owning pointers in the vector. If so, be careful to make Diagram have the correct destructor, and make it non-copy-constructable, and non-copy-assignable.

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

Sidebar

Related Questions

I'm learning Sockets programming in Java. I'm trying to solve a problem that consists
I'm trying to solve the following problem in Redis. I have a list that
I'm trying to solve a problem (in php, but programming language doesn't matter). I'm
I am currently trying to solve a problem on one of the online programming
I'm trying to solve a programming puzzle and running up against some difficulty. It's
I am trying to solve the following question which was part of a programming
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
I'm trying to solve a codechef beginner problem - Enormous Input Test . My
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the

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.