簡體   English   中英

如何在同一個 class 中轉發聲明 function?

[英]How to forward declare a function within the same class?

有一個 class A ,其中所有聲明和定義都放在里面,如下所示:

class A
{
  void f(); // forward declaration for a lengthy method
  ...
  void g()
  {
    f();    // call the above forward-declared method
  }
  void f()  // definition of a lengthy method
  {
    ...
  }
}

我不想像 A::f() 那樣將 f() 從 class header 中取出,而只想將所有源代碼保留在 class 中。 但是,當我編譯它時,我收到以下錯誤:

error C2535: 'void A::f()': member function already defined or declared
note: see declaration of 'A::f'

難道沒有辦法解決這個問題嗎?

您不能,也不需要轉發聲明成員 function。

Member function definitions are in a "complete class context", which means that they have access to all members of the class, even those declared after the function body.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM