分号 (;) 在 Class 个构造函数或析构函数之后
Semicolon(;) After Class Constructors or Destructors
我目前正在使用 Legacy Source 维护和研究该语言,我想澄清一些关于在 class 中使用分号的困惑。
这是让我感到困惑的地方。
class Base
{
public:
Base(int m_nVal = -1 ): nVal(m_nVal) {} // Confused here
virtual ~Base() {} // Confused here
public:
virtual void SomeMethod();
virtual int SomeMethod2();
protected:
int nVal;
};
class Derived : public Base
{
public:
Derived(int m_nVal):nVal2(m_nVal) {}; // Confused here
virtual ~Derived(){}; // Confused here
public:
virtual void SomeMethod();
virtual int SomeMethod2();
protected:/* Correction Here */
int nVal2;
};
我注意到有些 class destructors/constructors 后面有分号,有些则没有,我明白分号是 a 是 a语句终止符。我的问题是构造函数或析构函数之后的分号是否告诉编译器特定的内容?抑或是无关紧要的事情。
函数Base(int m_nVal = -1 ): nVal(m_nVal) {}
末尾的{}
意味着你有一个完整的函数定义,而不是像virtual void SomeMethod();
那样的简单声明
也许散开一点会更有辨识度:
Base(int m_nVal = -1 ):
nVal(m_nVal)
{
}
现在我们可以很容易地看到我们有完整的函数定义(带有 member initializer list 启动)并且函数不需要终止分号。
does the Semi-colon after the constructors or destructor tells something specific to the compiler?
它不会在成员函数定义之后(或之前)。†
在成员函数声明之后(但不是之前)它是强制性的。
'可能只是一个疏忽。
†:除非定义没有主体:
struct A {
A() = default; // Mandatory semicolon. Definition
~A() {} // Accessory semicolon. Definition
void foo(); // Mandatory semicolon. Declaration
};
我目前正在使用 Legacy Source 维护和研究该语言,我想澄清一些关于在 class 中使用分号的困惑。
这是让我感到困惑的地方。
class Base
{
public:
Base(int m_nVal = -1 ): nVal(m_nVal) {} // Confused here
virtual ~Base() {} // Confused here
public:
virtual void SomeMethod();
virtual int SomeMethod2();
protected:
int nVal;
};
class Derived : public Base
{
public:
Derived(int m_nVal):nVal2(m_nVal) {}; // Confused here
virtual ~Derived(){}; // Confused here
public:
virtual void SomeMethod();
virtual int SomeMethod2();
protected:/* Correction Here */
int nVal2;
};
我注意到有些 class destructors/constructors 后面有分号,有些则没有,我明白分号是 a 是 a语句终止符。我的问题是构造函数或析构函数之后的分号是否告诉编译器特定的内容?抑或是无关紧要的事情。
函数Base(int m_nVal = -1 ): nVal(m_nVal) {}
末尾的{}
意味着你有一个完整的函数定义,而不是像virtual void SomeMethod();
也许散开一点会更有辨识度:
Base(int m_nVal = -1 ):
nVal(m_nVal)
{
}
现在我们可以很容易地看到我们有完整的函数定义(带有 member initializer list 启动)并且函数不需要终止分号。
does the Semi-colon after the constructors or destructor tells something specific to the compiler?
它不会在成员函数定义之后(或之前)。†
在成员函数声明之后(但不是之前)它是强制性的。
'可能只是一个疏忽。
†:除非定义没有主体:
struct A {
A() = default; // Mandatory semicolon. Definition
~A() {} // Accessory semicolon. Definition
void foo(); // Mandatory semicolon. Declaration
};