2014-05-03 48 views
-6

当在C++声明的方法做你这样做例如如下(如果类名称是词法分析器例如):C++函数不同格式

bool Lexer :: IsDigit() 
{ 
//code 
} 

bool isDigit() 
{ 
//code 
} 

因为我http://www.cplusplus.com/doc/tutorial/functions/

+0

你迷惑声明,定义和内联定义是什么? –

+2

Nah。他对自由函数没有任何线索,我的猜测 – sehe

+1

您是否来自Java? C++具有(免费)功能和方法。 – keyser

回答

2

喜欢这样:

我与第一个样式但是我在这个网站找到的最后一个,以及诸如使用
struct Lexer 
{ 
    bool IsDigit() { return true; } 
    // ... 
}; 

或者是这样的:

struct Lexer 
{ 
    bool IsDigit(); 
    // ... 
}; 

bool Lexer::IsDigit() { return true; } 

第一个版本包括在类定义内的成员函数定义。后者只将成员函数声明放在类定义中,但将成员函数定义保留在外(或“脱序”)。

0

第二个例子是要么

a)一种自由站立功能或
b)一种内嵌方法定义(如果它是一类体内)。

第一个示例是外部方法定义。

class Lexer { 
    bool IsDigit(); // method declaration 
} 
bool Lexer :: IsDigit() { return false; } // method definition 

bool IsDigit() { return false; } // free-standing function 

class Lexer2{ 
    bool IsDigit { return false; } // inline method definition 
} 

它们可以被称为像这样:

Lexer l; 
l.isDigit(); // calls the first method 
isDigit(); // calls the free-standing function 
Lexer2 l2; 
l2.IsDigit(); // calls the latter method 
+0

愿意解释为什么? – Appleshell

+0

你为什么认为这是错的?我刻意留下了回报声明,因为这不是这个答案的意思。如果你认为他们在问题的背景下至关重要,只需编辑它们。或者你的意思是因为它是私密的? – Appleshell

+0

这不是一个SSCCE,这是* only *旨在显示问题中提到的功能的“格式”。 – Appleshell