Why am I not allowed to use the following syntax to call a constructor of a member object/different class in the body of the constructor of a class?
class Circle {
double radius;
public:
Circle(double r) : radius(r) { }
double area() {return radius*radius*3.14159265;}
};
class Cylinder {
Circle base;
double height;
public:
Cylinder(double r, double h) {
base(r);
height = h;
}
double volume() {return base.area() * height;}
};
By the way I know I can call Circle::Circle(double)
via Cylinder(double,double)
using member initialization list like Cylinder(double r,double h) : base(r), height(r) {}
but still what's wrong with the former method that the compiler is generating this error?