So I recently saw this snippet on the internet:
class Human
{
int age;
public:
constexpr Human(int humansAge) : age(humansAge) {}
constexpr int GetAge() const { return age; }
};
int main(int argc, char const *argv[])
{
constexpr Human somePerson(15);
const int hisAge = somePerson.GetAge();
return 0;
}
Now I've compared this class with and without the keyword constexpr on compiler explorer and I saw that the constructor didn't have it's instructions on the right side, but the function GetAge had them, Now I have some questions:
- What is
constexprdoing here to the constructor and the functionGetAge? - What is the role of the
constkeyword after the prototype of GetAge(), does it mean that age will be returned as a const? - What happens if I declare
int hisAgeas not being a const?
Thanks in advance.