How to check data type in C++? -


i'm new c++, i've been using python. i'm trying check type of variable of value stored in objects i'm working on. remember in python there comand isinstance use condition run commands, if next value string, a, , if it's int b.

is there way check what's data type on variable in c++?

example:

in python had array math operation, each character in field

[3,"+",2] 

as read array separate ints strings isinstance command

if isinstance(list[0],int):         aux1.append(list[0])         list=list[1:]      else:         if isinstance(lista[0],str):             aux2.append(list[0             list=list[1:] 

now in c++ need similar, time each character in node linked list , again, need separate them, ints in linked list, , strings in linked list

what seem struggling c++ statically , (relatively) typed language. discussion each of these terms mean refer this other question, it's explained there better could.

first of should sure need things way you're trying currently. not try write python style code.

that said, there 2 different approaches can achieve behavior that's similar python (dynamically typed, duck typing , relatively weak typing) allows do:

  1. use c++'s builtin dynamic type mechanisms. therefore, need create called polymorphic base class, class has @ least 1 virtual member function (the destructor works if don't have defined interface - must virtual avoid nasty issues). short example:

    struct value {   virtual void write_to(std::ostream &) const = 0;   virtual void read_from(std::istream &) = 0;   virtual ~value() {} // absolutely required!!! }; struct number : public value {   int data;   void write_to(std::ostream & stream) const {     stream << "<number " << data << ">";   }   void read_from(std::istream & stream) {     stream >> data; // not same format write_to, shame on me   }   // implicit destructor fine }; struct string : public value {   std::string data;   void write_to(std::ostream & stream) const {     stream << "<string " << data.size() << " " << data << ">";   }   void read_from(std::istream & stream) {     stream >> data; // not same format write_to, shame on me   } }; 

    using can example store values actual type can let user decide:

    std::vector<std::unique_ptr<value>> values; while (wantstoentermorevalues) {   std::string choice = ask("what type of value want enter?");   std::unique_ptr<value> value;   if (choice == "string") {     value = std::make_unique<string>();   } else if (choice == "number") {     value = std::make_unique<number>();   } else {     // launch apocalypse   }   value->read_from(std::cin);   values.push_back(value); } 

    this extensible more types. note in order use c++'s builtin dynamic typing need go without value semantics, instead use reference semantics, either using real references, or (in cases ownership must transferred, in above example values vector) using pointers.

    the dynamic_cast approach works similar this, except you're using runtime type information more explicitly , don't need unified interface (but have more work in order maintain code).

  2. use union language feature. has become possible c++11, union members may non-trivially construable:

    enum class type {   number, string }; struct value {   type type;   union {     std::string string;     int number;   };   value(std::string const & s) : type(type::string), string(s) {}   value(int n) : type(type::number), number(n) {}   value(value const & v) : type(v.type) {     switch (type) {       case type::number: number = v.number; break;       case type::string: new (&string) std::string(v.string); break;       default: break; // launch nuclear missiles     }   }   ~value() {     switch (type) {       case type::string: string.~std::string(); break;       default: break;     }   } }; 

    as can see, quite lot of work. approach can use value semantics, cannot extend values support more types. moreover, due using union, you're going waste memory.

bottom line: need implement behavior on own, can in way want behave. example: implement assignment operators operator=(value const &) implicit type conversions. can use implementations boost, boost::any or boost::variant.

i'd reference 2 answers i've written on site same subject, perhaps they're helpful you:

also c code relevant, because tries solve same issue: https://stackoverflow.com/a/35443434/1116364

note: code in answer written straight out of memory , not tested. serves demonstration of basic techniques.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -