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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:56:17+00:00 2026-06-09T14:56:17+00:00

Coming from my background in dynamic languages, I find I have a problem expressing

  • 0

Coming from my background in dynamic languages, I find I have a problem expressing my intentions in a statically-typed language such as C++.

I’m designing a preference system for my application. As every preference will have a few associated values (the default value, limits, an observer function…) I decided to encapsulate each preference in an object of its own. Here’s my first draft:

class Preference    // purely abstract class
{
    parseFromString(String s) = 0;
    get() = 0;
    void set(newVal) = 0;
private:
    // internal data
};

Now I need to create a few derived classes, like IntPreference, FloatPreference and StringPreference. Here’s how their declaration would look like:

class IntPreference : Preference          class StringPreference : Preference
{                                         {
    int parseFromString(String s);            String parseFromString(String s);
    void set(int newVal);                     void set(String newVal);
    // etc.                                   // etc.
}                                         }

Now that the set() method takes an int parameter in the class IntPreference and a String parameter in StringPreference, there’s no way to declare this function in the base class. The same goes with the return value of parseFromString(). I understand this is impossible to do in C++, because functions with the same name and different parameter types in a derived class just overshadow, not override their ancestors. Again, this is how I would express myself in a dynamic language, what’s the correct pattern in C++?

EDIT: Sorry, I forgot to mention I need a base class to store them all in a hash table:

Hash(const char *name, Preference pref);
  • 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-09T14:56:19+00:00Added an answer on June 9, 2026 at 2:56 pm

    What you have now, is a poor boost::any class and you maybe should
    simply just use that.

    Your parseFromString() member function is dubious. You use the
    dynamic type to decide what to parse out of the string, something that
    always has to be known statically.

    class my_any {
    public:
      template<typename T>
      explicit // don't rely on conversions too much
      my_any(const T& t) : x_(t) {}
    
      // might throw if the cast fails
      template<typename T>
      T& get() { return boost::any_cast<T&>(x_); }
    
      // also maybe move semantics
      template<typename T>
      set(const T& t) { x_ = t; }
    private:
      boost::any x_;
    };
    
    // usage:
    my_any m;
    m.set(23);
    try {
      int& x = m.get<int>();
    catch(boost::bad_any_cast& ex) {
      // ...
    }
    
    // for setting things from string just do 
    // the right thing at the call site of set
    

    If you dislike templates you can simply provide a few defaults:

    my_any::getInt(); my_any::getString();
    

    EDIT: If boost::any is too generic for you and you want to limit
    your construct to a certain set of values use
    boost::variant. Although a variant has a larger impact on compile
    time and can be quite hard to use for a beginner.

    EDIT2: The hash table problem:

    typedef boost::unordered_map<std::string, my_any> preference_table;
    preference_table t;
    // i added a template constructor to my_any
    t.insert(std::make_pair("Foobar", my_any(23)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coming from a statically-typed background and investigating functional programming, but I'm not entirely
I'm coming from a background in languages like Actionscript 3 where we have a
I am coming from C# background and I have hard time figuring out how
I'm coming from a C++ background to python I have been declaring member variables
I have read a bit about casting in C++. Coming from a C background,
I am coming from CVS background. Currently, I have 2 mercurial repositories developed parallel.
I am coming from iOS background and starting to learn Cocoa. On iOS unless
Background: I'm coming from Java background so not too familiar with Javascript. We are
I'm coming from a background in ColdFusion, and finally moving onto something modern, so
I've recently been learning vb.net, coming from a background in java and c. I'm

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.