creating a circle class and initializing the private member?

Are you allowed to initialize the private members in the class directly like i did below? ............. because i thought the nothing is created in memory until you create the object..... so can somebody explain this please?

1
2
3
4
5
6
class Circle
{
    private:
       double radius;
       double pi = 3.14;
}
Last edited on
> because i thought the nothing is created in memory until you create the object

You are right. The = 3.14 is an initializer. When an object is created, constructor(s) can use the information to initialize the member variable (if it has not been explicitly initialized by the code in the constructor).

C++11 in-class member initializers
http://www.stroustrup.com/C++11FAQ.html#member-init
Topic archived. No new replies allowed.