-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C.45 不要定义仅初始化数据成员的默认构造函数,而应该使用成员初始化器(C++11)
- Loading branch information
1 parent
e865062
commit 64c0deb
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
|
||
class WidgetImpro | ||
{ | ||
private: | ||
int width{640}; | ||
int height{480}; | ||
bool frame{false}; | ||
bool visible{true}; | ||
|
||
int getHeight(const int __width) { return __width * 3 / 4; } | ||
|
||
public: | ||
WidgetImpro() = default; | ||
|
||
explicit WidgetImpro(const int __width) noexcept : width(__width), height(getHeight(__width)) {} | ||
|
||
WidgetImpro(const int __width, const int __height) noexcept : width(__width), height(__height) {} | ||
|
||
friend std::ostream & operator<<(std::ostream & __os, const WidgetImpro & __widget) | ||
{ | ||
__os << std::boolalpha << __widget.width << " * " << __widget.height | ||
<< ", Frame: " << __widget.frame | ||
<< ", Visible: " << __widget.visible; | ||
|
||
return __os; | ||
} | ||
}; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
WidgetImpro wImproVGA; | ||
|
||
std::cout << wImproVGA << '\n'; | ||
|
||
return 0; | ||
} |