上课构造函数或破坏者之后的分号(;)

发布于 2025-01-31 06:34:07 字数 654 浏览 2 评论 0原文

我目前正在使用传统来源维护和研究该语言,我想清除对班级中半彩的使用的混乱。

这是混乱使我震惊的地方。

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;
};

我已经注意到,某些班级驱动器/构造师之后有半彩色,其中一些人没有,我确实知道,半彩是一个声明终结者。我的问题是,在构造函数或驱动器之后的半彩色告诉编译器特定的内容?还是不是真正重要的事情。

I am currently maintaining and studying the language using a Legacy Source,I want to clear up some confusion on the use of semi-colons inside a class.

Here is the bit where confusion strikes me.

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;
};

I have noticed that some of the class destructors/constructors have a Semi-colon after them and some of them don't, I do understand that the a semi-colon is a is a statement terminator. My question is does the Semi-colon after the constructors or destructor tells something specific to the compiler? or is it something that doesn't really matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2025-02-07 06:34:08

{}在函数末尾base(int m_nval = -1):nval(m_nval){}表示您对函数有完整的定义,而不是一个函数的定义单纯的声明如虚拟void somemethod();

也许当分布得更好时,它会更容易识别:

Base(int m_nVal = -1 ): 
    nVal(m_nVal) 
{
}

现在我们可以轻松地看到我们具有完整的函数定义(使用a 成员初始化器列表启动),功能永远不需要终止半元素。

The {} at the end of the function Base(int m_nVal = -1 ): nVal(m_nVal) {} means you have a complete definition of a function, not a mere declaration like virtual void SomeMethod();

Perhaps it would be more recognizable when spread out a bit better:

Base(int m_nVal = -1 ): 
    nVal(m_nVal) 
{
}

Now we can easily see we have the full function definition (with a member initializer list to boot) and functions never require a terminating semicolon.

萧瑟寒风 2025-02-07 06:34:08

构造函数或驱动器之后的半彩色会告诉编译器特定的内容吗?

(或之前)成员函数定义没有。†
在(但不是之前)成员函数声明之后,这是强制性的。
'可能只是一个监督。

†:除非定义没有任何身体:

struct A { 
  A() = default; // Mandatory semicolon. Definition
  ~A() {}        // Accessory semicolon. Definition
  void foo();    // Mandatory semicolon. Declaration
};

does the Semi-colon after the constructors or destructor tells something specific to the compiler?

After (or before) a member function definition it does not.†
After (but not before) a member function declaration it is mandatory.
'Probably just an oversight.

†: Unless the definition has no body:

struct A { 
  A() = default; // Mandatory semicolon. Definition
  ~A() {}        // Accessory semicolon. Definition
  void foo();    // Mandatory semicolon. Declaration
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文