[英]Clarification on C++ constructor syntax
我很好奇是否可以對 C++ 中的大量構造函數語法提供任何澄清。 我見過在類內和類外聲明的構造函數。 我還通過更常見的方法(例如x = y
或this-> x = y
)以及通過初始化列表(例如Point(int i = 0):x(i) {}
看到了變量初始化。
我的問題是在某些情況下是否更適合使用一種構造函數而不是另一種,或者是在 class 內部還是外部聲明構造函數。 他們有一些常用的語法指南嗎?
我覺得在 class 本身內部擁有構造函數的析構函數和其他方法的原型通常是一個好主意,如果它們屬於同一個 class。 它更高效,對程序員更友好。 例如:我會做這樣的事情:
class decl ....{
private:
members var.. methods' prototype..
public:
decl(); default cons prototype
decl(type para....); parameterized cons prototype;
~decl();
some methods' prototypes..
};
然后你總是可以提前 go 並使用它們帶一個參數將值分配給即時變量,例如:
decl::decl(can user the same name or different ){
if using the same name
this -> sameName = sameName;
otherwise,
instant data variable name = assign the value,
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.