Question in initializing a constructor

When I initialize my constructor like this :

Person::Person(std::string newName, int newAge): name(name), age(age) {}

And have..
std::string name; //and
int age;
..declared in my Person class declaration.

And use a display function:

void Person::display(){
std::cout << "Name: " << name << "." <<std::endl << "Age: " << age <<std::endl;
}

And then pass values in main:

Person Person1("Bobby", 42);
Person1.display();

it printed:
Name: . //why is it blank?
Age: 0xfjsdlfkjsdflksj //and the reference info for something.


please help!!
Person::Person(std::string newName, int newAge): name(name), age(age) {}

Your parameters are named 'newName' and 'newAge'

You are initializing your members with 'name' and 'age'. Those are not your parameters. So you are effectively initializing your members with themselves instead of the parameters.

To be honest, I'm a little surprised that compiles. You'd think you'd at least get a warning or something.
Wow, I can't believe I missed that ! I looked right over it. Thanks for your speedy and clear response.
This is my first time using this site; You've been helpful !
Topic archived. No new replies allowed.