Skip to content

Commit

Permalink
refactor: add initialization when the variable is declared.
Browse files Browse the repository at this point in the history
  • Loading branch information
wgy8283335 committed Mar 2, 2024
1 parent 073e152 commit 97e8d91
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion include/effective_c++/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace effective {

private:
virtual std::string onTick() const;
int tick_frequency;
//> 成员变量初始化的顺序为:先声明时初始化,然后初始化列表初始化,最后构造函数初始化。
//> 如果调用了一个类的默认构造函数,那么这个变量的值则来自于声明初始化。如果声明时
//> 未初始化,则此变量值为该类型的默认值。
int tick_frequency{1};
bool is_running{true};
};
} // namespace effective
Expand Down
5 changes: 4 additions & 1 deletion include/effective_c++/home/home.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ namespace effective {
std::string boardcastAddress() const;

protected:
std::string address_;
//> 成员变量初始化的顺序为:先声明时初始化,然后初始化列表初始化,最后构造函数初始化。
//> 如果调用了一个类的默认构造函数,那么这个变量的值则来自于声明初始化。如果声明时
//> 未初始化,则此变量值为该类型的默认值。
std::string address_ = "";

private:
//> doHealthValue是private级别,派生类访问不到,但是派生类可以重写属于自己的函数。
Expand Down
5 changes: 5 additions & 0 deletions test/source/home_factory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ TEST_CASE("HomeFactory") {
CHECK(one_cave->GetAddress("decorate ") == "decorate ParaMountain");
CHECK(one_cave->GetAddress() == "ParaMountain");
CHECK(one_cave->boardcastAddress() == "This from home begin send address Tick begin My cave address is : ParaMountainMy cave address is : ParaMountainMy cave address is : ParaMountainend.");
auto one_nest = home_factory.makeHome("Nest");
CHECK(one_nest->draw(2) == "let paint the house as Blue");
CHECK(one_nest->healthValue() == "before check health of full Nest after.");
CHECK(one_nest->GetAddress("decorate ") == "decorate ");
CHECK(one_nest->boardcastAddress() == "This from home begin send address Tick begin My nest address is : My nest address is : My nest address is : end.");
}

0 comments on commit 97e8d91

Please sign in to comment.