Hello. I'm going back over some C++ stuff. The following program is the usual way I might get user input into a private member of a class.
Code:
#include <iostream>
class Account {
private:
int a{ 0 };
public:
Account() {}
void setA(int a) {
Account::a = a;
}
int getA() {
return a;
}
};
int main() {
Account p;
std::cout << "Enter int: ";
int copy;
std::cin >> copy;
p.setA(copy);
}
I would like to avoid creating the copy variable though. It seems to me like it is a wasted step when I should be able to just put cin strait into the set function. I know the reason why I can't is that the data from cin is a stream which needs to be converted into an int. Is it possible to do this without creating a variable to do only this?
Code:
int main() {
Account p;
std::cout << "Enter int: ";
p.setA(std::cin.toInt);
}