0

Was wondering - is it possible to reassign data types in C++, just like in Python?

For example:

int age = 18;
//no longer need this cached in memory...
string age = "I'm a different data type now...";
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    https://en.cppreference.com/w/cpp/utility/variant – oakad Feb 18 '21 at 02:52
  • https://stackoverflow.com/questions/38537156/changing-variable-types-after-initialization-c hope this helps. – Ashish M J Feb 18 '21 at 02:52
  • 1
    Recommendation: don't try to use C++ the way you would Python. You'll wind up discarding C++'s strengths and wind up with programs that will likely be inferior in most respects, including performance, to the Python version. – user4581301 Feb 18 '21 at 03:03
  • 1
    Good find, @AshishMJ – user4581301 Feb 18 '21 at 03:04
  • 1
    What's the problem you're trying to solve? There's probably a way of solving it that's 50x simpler than using `std::variant`. – alter_igel Feb 18 '21 at 03:35

0 Answers0