Is it possible to change the data type of a class member after its initialization? Say, redefine a double named "Foo" to a string?
No. Types are fixed at compile time. If you want to switch between double and a string perhaps reach for a std::variant :
std::variant<double, std::string> val = 1.0;
val = std::string("hello");
As answer to your comment, you should use std::optional
std::optional<int> Do(int x, int y)
{
if ( x == y )
{
return 42;
}
return {};
}
int main()
{
auto ret = Do(3,2); // << exchange your test data here!
if ( ret )
{
std::cout << "Got an answer" << ret.value() << std::endl;
}
else
{
std::cout << "No answer" << std::endl;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.