c++ - CIN to either string or int variable then to overloaded constructor functions -


total newb here, go easy :) i've googled , can't seem find elegant solution this. i'm doing coding learn few concepts.

i have class - called 'sally' has constructor overloaded twice i.e. sally.cpp is:

#include "sally.h" #include <iostream> #include <string> using namespace std;   sally::sally() { } sally::sally(int x) {     inputvarint = x;     cout << "you have int of value: " << inputvarint << endl; }  sally::sally(string y) {     inputstring = y;     cout << "you have string is: " << inputstring << endl; } 

in main.cpp if create object sally myobj; , call function via object, i.e. sally.myobj(55) correct constructor firing off , telling me have int of value whatever, or if go sally.myobj("johhny") other constructor going , telling me have string says "johnny", part fine.

what know is, there elegant way can use cin take input user, , either pass straight object - without using variable ( i've tried , can't work ).

basically, i'd user able enter either string, or int , use in sally.myobj(); call, , let constructor(s) work of figuring out type of data is.

things like: auto x = 0; , cin >> x don't work initial declaration int, stays that.

is there way declare variable without type , assign type based on input cin?

apologies if answer blindingly obvious, i'm @ beginning of journey ( i'm tutorial 40 of guy: http://www.youtube.com/watch?v=tvc1wcdv1xu ) , reading c++ primer stanley lippman ( 5th edition ) , object-oriented thought process matt weisfield. resources @ present ( , google ).

thanks in advance

seb

edit:

after seeing of responses below, helped me figure out can/can't done , i've tackled way using parsing function utilises regex, , if string content deemed int converts , places new variable , off constructor:

sally.cpp:

#include "sally.h" #include <iostream> #include <string> #include <regex> using namespace std;  sally::sally() { } sally::sally(int x) {     inputvarint = x;     cout << "constructor output: have int of value: " << inputvarint << endl; }  sally::sally(string y) {     inputstring = y;     cout << "constructor output: have string is: " << inputstring << endl; } void sally::stringparser(string x){  regex e("^[0-9]+$"); //test if string starts ends , has whole lot of numbers in between, i.e. , int of length  bool match = regex_match(x, e);  if(match){     cout << "you have int" << endl;     int newvar = atoi(x.c_str());     sally::sally(newvar); } else {sally::sally(x);}   } 

then sally myobj; , myobj.stringparser(x); whereby x input cin , parsing function takes care of rest , passes constructors.

i'm sure build out bit switch statement different regex's handle floats etc , create additional constructors. now, seem have found solution.

keen see if there issues approach.

i think c++ variables must have type. in case better save string , evaluate it. if number, convert int , pass constructor: if not number pass string constructor can check if number using this:

bool is_number(const std::string& s) {   std::string::const_iterator = s.begin();   while (it != s.end() && std::isdigit(*it)) ++it;     return !s.empty() && == s.end(); } 

from how determine if string number c++?

for converting string int, there various ways, can try one:

string str = "123"; int numb; istringstream ( str ) >> numb; 

so final idea like:

get input string if(input number) {   convert number;   call constructor; } else   call constructor string 

hope helped out.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -