Can I declare two variables in a conditional in C++. The compiler gave an error but still I think I should get an opinion:
int main()
{
double d1 = 0;
if((double d2 = d1) || (double d3 = 10))
cout << "wow" << endl;
return 0;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You can only do that for one variable
But i would declare them outside of the conditions. If you are scared about the scope, you can always limit it:
Of course, this evaluates the second initializer even if
d2evaluates to true. But i assume that’s not important.