Skip to content

Commit

Permalink
C.45 不要定义仅初始化数据成员的默认构造函数,而应该使用成员初始化器(C++11)
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseZ332623 committed Feb 24, 2024
1 parent e865062 commit 64c0deb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Binary file added bin/widgetImpro.exe
Binary file not shown.
37 changes: 37 additions & 0 deletions src/widgetImpro.cpp
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;
}

0 comments on commit 64c0deb

Please sign in to comment.