coming from a primarily python background I have somewhat struggled with working with types in C++.
I am attempting to initialise a class variable via one of several overloaded constructors that take different types as parameters. I have read that using the auto
keyword can be used for auto declaration of a variable, however in my case it will not be initialised till a constructor is chosen. However the compiler is not happy about not initialising value
.
class Token {
public:
auto value;
Token(int ivalue) {
value = ivalue;
}
Token(float fvalue) {
value = fvalue;
}
Token(std::string svalue) {
value = svalue;
}
void printValue() {
std::cout << "The token value is: " << value << std::endl;
}
};
In python this might look like:
class Token():
def __init__(self, value):
self.value = value
def printValue(self):
print("The token value is: %s" % self.value)
What is the right way of using the auto
keyword in this scenario? Should I use a different approach altogether?