I have a Client class, which receives an std::string variable called emailAdress. This is what the code looks like now, and it works:
private:
const std::string email;
Client::Client(std::string emailAddress) : email{emailAddress}
{
}
Now, I want to check if the email contains characters like an @ or a valid name with no strange characters. I want to do this with regex. Now, my question is, how do I initialize the const std::string email variable after changing the parameter variable? It says it doesn't want to because it is a const variable, that is why it is in the initialization list of the constructor right now.
email{SomeFunctionThatPerformsNecessaryChanges(emailAddress)}?constdata members come with several drawbacks and are generally not worth it. It almost always makes the type non-assignable and non-moveable. It is often best to maintain the invariance using the class interface. Make the memberprivateand don't provide a setter.