Say there is a class:
class person {
int age;
string name;
public:
person(int age,string name)
:age(age),name(name)
{}
};
I have the following questions:
- Can the constructor initializer(with the ":" syntax) be used somehow outside of classes?Is it just for classes?
- A variable declaration such as
int x(2)works for obvious reasons.x is an object with value of 2.But age(age),how does that work?Is the syntax made that way to make initialization easier? - Based on that,how come can the parameter age & member age have the same name and not confuse the compiler?Member age is in scope(as a private member),but parameter age is also in scope.When this happens with ordinary functions,the "localest" is the one that prevails.
Thanks!