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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:43:16+00:00 2026-05-27T00:43:16+00:00

I’m implementing a variant class (not using boost) and I’m wondering how you’d handle

  • 0

I’m implementing a variant class (not using boost) and I’m wondering how you’d handle the case where you’d store any of string, integer, or double and automatically convert it accordingly to desired type through ToString(), ToInt(), or ToDouble().

For example,

Variant a = 7;
cout << "The answer is" + a.ToString() << endl; // should print "The answer is 7"
a = "7.4";
double &b = a.ToDouble();
b += 1;
cout << a.ToString() << endl; // should print 8.4

ToXXX functions should return the reference of the type that you want to convert to. Right now, I have the code where it can return the same type as it was initially assigned to( Variant a = Int(7); a.ToInt() works) and raise exception when the assigned type is different from the one that you want to convert to.

Sorry using boost isn’t an option.

  • 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-27T00:43:16+00:00Added an answer on May 27, 2026 at 12:43 am
        #include <string>
        #include <iostream>
        class Variant{
        public:
            Variant(){
                data.type = UNKOWN;
                data.intVal = 0;
            }
            Variant(int v){
                data.type = INT;
                data.intVal = v;
            }
            Variant(double v){
                data.type = DOUBLE;
                data.realVal = v;
            }
            Variant(std::string v){
                data.type = STRING;
                data.strVal = new std::string(v);
            }
                //the missing copy constructor
                 Variant(Variant const& other)
                 {
                    *this = other;// redirect to the copy assignment
                  }
    
            ~Variant(){
                if(STRING == data.type){
                    delete data.strVal;
                }
            }
    
            Variant& operator = (const Variant& other){
                if(this != &other)
                {
                    if(STRING == data.type){
                        delete data.strVal;
                    }
    
                    switch(other.data.type){
                    case STRING:{
                            data.strVal = new std::string(*(other.data.strVal));
                            data.type = STRING;
                            break;
                        }
                    default:{
                            memcpy(this, &other, sizeof(Variant));
                            break;
                        }           
                    }
                }
                return *this;
            }
            Variant& operator = (int newVal){
                if(STRING == data.type){
                    delete data.strVal;
                }
                data.type = INT;
                data.intVal= newVal;
                return *this;
            }
    
            Variant& operator = (double newVal){
                if(STRING == data.type){
                    delete data.strVal;
                }
                data.type = DOUBLE;
                data.realVal= newVal;
                return *this;
            }
    
            Variant& operator = (std::string& newVal){
                if(STRING == data.type){
                    delete data.strVal;
                }
                data.type = STRING;
                data.strVal= new std::string(newVal);
                return *this;
            }
            operator int&() {
                if(INT == data.type)
                {
                    return data.intVal;
                }
                //do type conversion if you like
                throw std::runtime_error("bad cast");
            }
    
            operator double&()  {
                if(DOUBLE == data.type){
                    return data.realVal;
                }
                //do type conversion if you like
                throw std::runtime_error("bad cast");
            }
            operator std::string&() {
                if(STRING == data.type){
                    return *data.strVal;
                }
                throw std::runtime_error("bad cast");
            }
        private:
            enum Type{
                UNKOWN=0,
                INT,
                DOUBLE,
                STRING
            };
            struct{
                Type type;
                union 
                {
                    int intVal;
                    double realVal;
                    std::string* strVal;
                };
            } data;
        };
    
    
        int main(){
            Variant v("this is string");//string
            v=1;//int
            v=1.0;//double
            v=std::string("another string");//
            Variant v2; //unkown type
            v2=v;//string
            std::cout << (std::string&)v2 << std::endl;
            return 0;
        }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of 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.