我很好奇是否可以对C++中大量的构造函数语法提供任何澄清。我见过在类内和类外声明的构造函数。我还看到过通过更常见的方法初始化变量,比如x = y或this-> x = y,也有通过初始化列表进行初始化的,比如Point(int i = 0):x(i) {}。
我的问题是,在某些情况下,使用一种风格的构造函数比使用另一种风格的构造函数更合适,还是在类的内部或外部声明构造函数。对于这一点,他们有没有一些常用的语法准则?
发布于 2020-11-26 04:10:19
我觉得如果构造函数的析构函数和其他方法的原型属于同一个类,那么将它们放在类本身中通常是一个好主意。它效率更高,对程序员更友好。例如:我会这样做:
class decl ....{
private:
members var.. methods' prototype..
public:
decl(); default cons prototype
decl(type para....); parameterized cons prototype;
~decl();
some methods' prototypes..
};然后你可以继续使用它们,接受一个参数,将值赋给即时变量,如下所示:
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,
}https://stackoverflow.com/questions/65010816
复制相似问题