2013-09-26 45 views
0

例如源文件中定義的方法是否可以內聯?

// a.h 
strcut A 
{ 
    void simpleMethod(); 
    void anotherMethod() {...; simpleMethod(); ...;} 
}; 

// a.cpp 
#include "a.h" 
void A::simpleMethod() { one_line_simple_implementation; } 

我的問題是:simpleMethod()anotherMethod()由現代編譯器的優化內聯?

回答

1

在這種情況下,是的。如果a.cpp包括a.h(我想是的)。只要編譯器可以看到完整的定義。沒事。但是你需要這樣說。

inline  void A::simpleMethod() { one_line_simple_implementation; } 
0

可以使用inline void A::simpleMethod() { ... }或者,如果你不使用inline關鍵字,您可能會收到多個定義錯誤。 或者你可以做

class A 
{ 
    void foo() { ... }; 
}; 
相關問題