在构造函数中初始化,最佳实践? [英] Initializing in constructors, best practice?
本文介绍了在构造函数中初始化,最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在用C ++编程,我使用了两种方法:
I've been programming in C++ a while and I've used both methods:
class Stuff {
public:
Stuff( int nr ) : n( nr ) { }
private:
int n;
}
或
class Stuff {
public:
Stuff( int nr ) {
n = nr;
}
private:
int n;
}
注意:这与这,类似但不一样。
Note: This is not the same as this, similar but not the same.
最好的做法是什么?
推荐答案
请参阅常见问题10.6
这篇关于在构造函数中初始化,最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文