C++构造函数使用的多种方法
2020-12-13 03:24
标签:rect init form mes space radius following directly body Member initialization in constructors The constructor for this class could be defined, as usual, as: But it could also be defined using member initialization as: Or even: Note how in this last case, the constructor does nothing else than initialize its members, hence it has an empty function body. C++构造函数使用的多种方法 标签:rect init form mes space radius following directly body 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/11074537.html// classes and uniform initialization
#include
Rectangle rectb; // default constructor called
Rectangle rectc(); // function declaration (default constructor NOT called)
Rectangle rectd{}; // default constructor called
When a constructor is used to initialize other members, these other members can be initialized directly, without resorting to statements in its body. This is done by inserting, before the constructor‘s body, a colon (:) and a list of initializations for class members. For example, consider a class with the following declaration:class Rectangle {
int width,height;
public:
Rectangle(int,int);
int area() {return width*height;}
};
Rectangle::Rectangle (int x, int y) { width=x; height=y; }
Rectangle::Rectangle (int x, int y) : width(x) { height=y; }
Rectangle::Rectangle (int x, int y) : width(x), height(y) { }
上一篇:window7_64安装STAF
下一篇:Python 多线程、多进程