N
newbiecpp
I have a simple class:
class Point {
public:
Point() : xval(0), yval(0) {}
Point(int x, int y) : xval(x), yval(0) {}
private:
int xval, yval;
};
The Point is the member of another class:
class UP {
public:
UP() : u(0) {} // line 1
UP(int x, int y) : p(x, y), u(0) {}
private:
int u;
Point p;
};
My question is:
1) How member object is constructed (in this case, Point p)? Does the
compiler initializes member object, then calls constructors, or member
objects are initialized by constructors?
2) If member objects are initialized by constructors, does the code in line
1 should be:
UP() : p(), u(0) {} or UP() : p, u(0)
if line 1 is correct, how p is initialized?
Thank!
class Point {
public:
Point() : xval(0), yval(0) {}
Point(int x, int y) : xval(x), yval(0) {}
private:
int xval, yval;
};
The Point is the member of another class:
class UP {
public:
UP() : u(0) {} // line 1
UP(int x, int y) : p(x, y), u(0) {}
private:
int u;
Point p;
};
My question is:
1) How member object is constructed (in this case, Point p)? Does the
compiler initializes member object, then calls constructors, or member
objects are initialized by constructors?
2) If member objects are initialized by constructors, does the code in line
1 should be:
UP() : p(), u(0) {} or UP() : p, u(0)
if line 1 is correct, how p is initialized?
Thank!